Overview
Electric current is the flow of electric charge in a medium. Its strength depends on the potential difference and the electrical properties of the medium. Amperes (A) is the unit that measures electric current.
An electric current is classified into two types: direct and alternating. In direct current (DC), the electrons flow in one direction only, going from the positive charge to the negative charge. On the other hand, the electrons in alternating current flow in one direction, then in the opposite direction.
In this project, we will measure the alternating current flow in a particular load. To measure the current, an ACS712 current sensor is used. The gathered data will be sent to Blynk. According to its founders, Blynk is an Internet of Things platform with a drag-n-drop mobile application builder that allows the users to visualize sensor data and control electronics remotely in minutes.
Hardware Used
- Arduino Uno R3 (Robotdyn)
- ACS712 (5A) Current Sensor
- SIM800C GSM Module
- Lamp
- Breadboard
- Jumper Wires
Software Used
- Arduino IDE
- Blynk mobile application (Android or iOS)
Arduino Libraries Used
- TinyGsmClient.h
- BlynkSimpleSIM800.h
- SoftwareSerial.h
Application Description
This project measures the current flow across a load. In this case, a lamp is connected to the mains voltage, and the current flow is measured. Then, the obtained data is transmitted to the Blynk cloud by using the SIM800C GSM module. The user can access the data through the mobile application of Blynk.
Set-Up the Hardware
Figure 1 – Schematic Diagram
Figure 2 – SIM800C GSM Module Front View
Figure 3 – SIM800C GSM Module Back View
Figure 4 – ACS712 Current Sensor
Figure 1 shows the connections between the Arduino Uno, SIM800C GSM module, and the ACS712 current sensor. Furthermore, the RX and TX pins of the GSM module are connected to the assigned TX and RX pins of the Arduino Uno, respectively.
Also, it is vital to be extra cautious when connecting the ACS712 current sensor and the lamp because the lamp is connected to the mains voltage of 220V. Soldering together the connections of the lamp to the mains voltage is an important task to ensure safety when used.
Set-Up the Software
#define BLYNK_PRINT Serial #define TINY_GSM_MODEM_SIM800 #include <TinyGsmClient.h> #include <BlynkSimpleSIM800.h> #include <SoftwareSerial.h> SoftwareSerial SerialAT(2, 3); // RX, TX char auth[] = "YOUR AUTH TOKEN "; //Auth Token // Your GPRS credentials // Leave empty, if missing user or pass char apn[] = ""; char user[] = ""; char pass[] = ""; const int sensorIn = A0; int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module double Voltage = 0; double VRMS = 0; double AmpsRMS = 0; TinyGsm modem(SerialAT); BlynkTimer timer; void setup() { // Debug console Serial.begin(9600); delay(10); // Set GSM module baud rate SerialAT.begin(9600); delay(3000); // Restart takes quite some time // To skip it, call init() instead of restart() Serial.println("Initializing modem..."); modem.init(); Blynk.begin(auth, modem, apn, user, pass); timer.setInterval(2000L, sendSensor); } void loop() { Blynk.run(); timer.run(); } void sendSensor() { Voltage = getVPP(); VRMS = (Voltage / 2.0) * 0.707; AmpsRMS = (VRMS * 1000) / mVperAmp; Serial.print(AmpsRMS); Serial.println(" Amps RMS"); Blynk.virtualWrite(V5, AmpsRMS); } float getVPP() { float result; int readValue; //value read from the sensor int maxValue = 0; // store max value here int minValue = 1024; // store min value here uint32_t start_time = millis(); while ((millis() - start_time) < 1000) //sample for 1 Sec { readValue = analogRead(sensorIn); // see if you have a new maxValue if (readValue > maxValue) { /*record the maximum sensor value*/ maxValue = readValue; } if (readValue < minValue) { /*record the maximum sensor value*/ minValue = readValue; } } // Subtract min from max result = ((maxValue - minValue) * 5.0) / 1024.0; return result; }
The code needed for this project is shown above. It is divided into five major parts. These are the declaration of variables, setup function, loop function, sendSensor() function and the getVPP() function. These five major parts of the code will be thoroughly explained below
#define BLYNK_PRINT Serial #define TINY_GSM_MODEM_SIM800 #include <TinyGsmClient.h> #include <BlynkSimpleSIM800.h> #include <SoftwareSerial.h> SoftwareSerial SerialAT(2, 3); // RX, TX char auth[] = "YOUR AUTH TOKEN "; //Auth Token // Your GPRS credentials // Leave empty, if missing user or pass char apn[] = ""; char user[] = ""; char pass[] = ""; const int sensorIn = A0; int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module double Voltage = 0; double VRMS = 0; double AmpsRMS = 0; TinyGsm modem(SerialAT); BlynkTimer timer;
The snippet of code shown above is the first major part of the program. This is where the variables are first initialized and the libraries are defined and declared. In this project, the program uses three libraries, These are the TinyGsmClient, BlynkSimpleSIM800, and the SoftwareSerial library.
The TinyGsmClient library is used for the SIM800C GSM module. This library contains the code needed for initializing the modem used for transmission and reception of data. On the other hand, the BlynkSimpleSIM800 library is utilized to connect the GSM module and the Blynk cloud. The Blynk is where the data is stored and accessed using its mobile application. Lastly, the SoftwareSerial library is used for serial communication. Serial communication is a type of communication wherein data is transmitted and received using a communication channel one bit at a time.
Also, the declaration of pins is found here. In this project, digital pins 2 and 3 of the Arduino Uno are used for serial communication. In addition, the analog pin A0 is utilized for the acquisition of data from the ACS712 current sensor.
Lastly, the authentication token needed to connect this project to the Blynk server is located in this part. The token is acquired through an e-mail sent by Blynk. This will be discussed in the latter parts of this tutorial.
void setup() { // Debug console Serial.begin(9600); delay(10); // Set GSM module baud rate SerialAT.begin(9600); delay(3000); // Restart takes quite some time // To skip it, call init() instead of restart() Serial.println("Initializing modem..."); modem.init(); Blynk.begin(auth, modem, apn, user, pass); timer.setInterval(2000L, sendSensor); }
The second major part of the code is the setup function. This function contains the framework of the code since this is where the important aspects of the code are assigned. These aspects include the baud rate, modem, and the timer. The baud rate is the speed of transmission of data in a certain communication channel. The modem is referred to as the GSM module, while the timer is used by Blynk as the clock which allows the data to be transmitted and received in a synchronous manner. In this project, the obtained data is transmitted to Blynk every 2 seconds.
void loop() { Blynk.run(); timer.run(); }
The third part – and perhaps the shortest one, is the loop function. The Arduino Uno does the processes involved in the loop function repeatedly. In this project, the processes involved in the loop function are the Blynk and timer. These two processes allow the Blynk and timer to operate per reiteration of the loop function.
float getVPP() { float result; int readValue; //value read from the sensor int maxValue = 0; // store max value here int minValue = 1024; // store min value here uint32_t start_time = millis(); while ((millis() - start_time) < 1000) //sample for 1 Sec { readValue = analogRead(sensorIn); // see if you have a new maxValue if (readValue > maxValue) { /*record the maximum sensor value*/ maxValue = readValue; } if (readValue < minValue) { /*record the maximum sensor value*/ minValue = readValue; } } // Subtract min from max result = ((maxValue - minValue) * 5.0) / 1024.0; return result; }
The fourth major part of the code is the getVPP() function. This function is used for measuring the current from the ACS712 current sensor. This is where the mathematical process of ratio and proportion is used, wherein the minimum and maximum value are compared to the acquired value from the sensor to get an approximate value of the peak-to-peak voltage. The conversion of current to voltage will be explained in the last part of the code.
void sendSensor() { Voltage = getVPP(); VRMS = (Voltage / 2.0) * 0.707; AmpsRMS = (VRMS * 1000) / mVperAmp; Serial.print(AmpsRMS); Serial.println(" Amps RMS"); Blynk.virtualWrite(V5, AmpsRMS); }
The final part of the code is the sendSensor() function. This function converts the acquired data from the previous function to the appropriate data. In this project, the obtained data is the peak-to-peak voltage. Converting it to the RMS current requires a mathematical process as shown above.
Lastly, this function allows the Arduino Uno to print the converted data to Blynk using the virtualWrite() function. It is important to know that this function should not be used in the loop function, as it would cause errors in sending the data to the server. This function uses virtual pins to transmit and receive data. Virtual pins are a concept invented by Blynk to display and send data from the hardware to Blynk.
In this project, it is vital to know the fundamentals of Blynk. According to its founders, Blynk is an Internet of Things platform with a drag-n-drop mobile application builder that allows the users to visualize sensor data and control electronics remotely in minutes. The Blynk mobile application is available on Google Play Store for Android users.
The first step in incorporating Blynk into this project is to create a new file for it. The picture shown above is the menu used to create the new file. The device should be the Arduino Uno and the connection type should be the GSM module.
After creating the file, an authentication token is sent to the user’s email. The email is asked when starting the app for the first time. The token should be included in the Arduino code, as shown in the earlier parts of this tutorial.
The picture shown above displays the workspace. This is where the user can add different components to display the gathered data in a way that is visually pleasing.
To add different widgets, click the add button in the upper right corner of the screen. The picture shown above displays the different widgets and components available for data display. In this project, the gauge is used to display the data since it varies per second and it is an analog value.
The gauge is shown above. It displays the current across the lamp through the different connections and code used.
Clicking the gauge displays this menu as shown above. This is where the user can assign the virtual pin and the reading rate. In this project, the V5 is utilized as the virtual pin. Also, the reading rate is at PUSH. This means that the Arduino will constantly send data to the Blynk server. Thus, the gauge changes its value for each time the Arduino sends data to the server.
Conclusion
This project utilizes the SIM800C GSM module to send data to the Blynk server. The Internet of Things (IoT) is an easy task thanks to the founders of Blynk. Anyone can use Blynk to send and receive data in any sensor and using any connection type.
Download Links
References
- http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-display-any-sensor-data-in-blynk-app
- http://henrysbench.capnfatz.com/henrys-bench/arduino-current-measurements/acs712-arduino-ac-current-tutorial/
The post Remote Current Consumption Monitoring of Appliances using ACS712, SIM800C, Arduino Uno and Blynk appeared first on CreateLabz.