How to set up smartphones and PCs. Informational portal
  • home
  • Televisions (Smart TV)
  • Digital voltmeter on Arduino connected to PC via serial port. Arduino secret voltmeter - measuring battery voltage by means of a microcontroller

Digital voltmeter on Arduino connected to PC via serial port. Arduino secret voltmeter - measuring battery voltage by means of a microcontroller

There are times when you want to check a voltage or a point in a circuit, but you don't have a voltmeter or multimeter on hand? Run to buy? It is long and expensive. Before you do that, how about making a voltmeter yourself? In fact, with simple components, you can make it yourself.

  • In the tutorial, we used an Arduino compatible board - SunFounder Uno / Mars (http://bit.ly/2tkaMba)
  • USB data cable
  • 2 potentiometers (50k)
  • LCD1602 - http://bit.ly/2ubNEfi
  • Development board - http://bit.ly/2slvfrB
  • Multiple jumpers

Before connecting, let's first take a look at how it works.

Use the SunFounder Uno board for the main part of the voltmeter data processing, LCD1602 as the screen, the potentiometer to adjust the LCD contrast, and the other one to separate the voltage.

When you rotate a potentiometer connected to the Uno board, the potentiometer resistor changes, thereby changing the voltage across it. The voltage signal will be sent to the Uno board through pin A0, and the Uno will digitize the received analog signal and write to the LCD. So you can see the voltage value at the current resistance of the capacitor.

LCD1602 has two modes of operation: 4-bit and 8-bit. When the IO of the MCU is insufficient, you can choose 4-bit mode, which only uses pins D4 ~ D7.

Follow the table to connect them.

Step 4: connect potentiometer to LCD1602

Connect the middle pin of the potentiometer to the Vo pin on the LCD1602 and any of the other pins to GND.

Connect the middle pin of the potentiometer to pin A0 of SunFounder Uno and one of the others to 5V, while the other to GND.

Step 6: Load the code

Code like this:

#include / ********************************************** ***** / const int analogIn = A0; // potentiometer attach to A0 LiquidCrystal lcd (4, 6, 10, 11, 12, 13); // lcd (RS, E, D4, D5, D6.D7) float val = 0; // define the variable as value = 0 / ********************************** ****************** / void setup () (Serial.begin (9600); // Initialize the serial lcd.begin (16, 2); // set the position of the characters on the LCD as Line 2, Column 16 lcd.print ("Voltage Value:"); // print "Voltage Value:") / ******************* ********************************** / void loop () (val = analogRead (A0); // Read the value of the potentiometer to val val = val / 1024 * 5.0; // Convert the data to the corresponding voltage value in a math way Serial.print (val); // Print the number of val on the serial monitor Serial.print ("V"); // print the unit as V, short for voltage on the serial monitor lcd.setCursor (6,1); // Place the cursor at Line 1, Column 6. From here the characters are to be displayed lcd.print (val); // Print the number of v al on the LCD lcd.print ("V"); // Then print the unit as V, short for voltage on the LCD delay (200); // Wait for 200ms)

Rotate the potentiometer to check the voltage on the LCD1602 in real time.

Here's a tricky thing. After I ran the code, symbols were displayed on the LCD. Then I adjusted the screen contrast (fade from black to white) by rotating the potentiometer clockwise or counterclockwise until the screen showed the characters clearly.

Take two batteries to measure their voltages: 1.5V and 3.7V. Disconnect the connection of the second potentiometer with pin A0 and GND, which means removing the potentiometer from the circuit. Clamp the end of the A0 wire to the anode of the battery and the GND circuit to the cathode. DO NOT plug them back in, otherwise you will get a short circuit on the battery. The 0V value is the reverse connection.

So the battery voltage is displayed on the LCD. There may be some error between the value and the nominal value as the battery is not fully charged. And that's why I need to measure the voltage in order to understand whether I can use the battery or not.

PS: If you have display problems - see this FAQ for LCDs - http://wiki.sunfounder.cc/index.php?title=LCD1602/I2C_LCD1602_FAQ.

This article shows you how to connect Arduino and PC and transfer data from ADC to PC. The Windows program is written using Visual C ++ 2008 Express. The voltmeter program is very simple and has a lot of room for improvement. Its main purpose was to show how to work with a COM port and exchange data between a computer and an Arduino.

Communication between Arduino and PC:

  • Reading from the ADC begins when the computer sends the Arduino commands 0xAC and 0x1y. at- ADC channel number (0-2);
  • Reading stops after Arduino receives 0xAC and 0x00 commands;
  • While taking readings, the Arduino sends commands 0xAB 0xaa 0xbb to the computer every 50 ms, where aa and bb are the maximum and minimum measurement results.

Arduino program

You can read more about serial communication at arduino.cc. The program is quite simple, most of it is occupied by working with a parallel port. After the end of reading data from the ADC, we get a 10-bit voltage value (0 × 0000 - 0 × 0400) in the form of 16-bit variables (INT). The serial port (RS-232) allows data transmission in packets of 8 bits. It is necessary to divide 16-bit variables into 2 parts of 8 bits.

Serial.print (voltage >> 8, BYTE);

Serial.print (voltage% 256, BYTE);

We shift the bytes of the variable 8 bits to the right and then divide by 256 and send the result to the computer.

Full source code for Arduino software you can download

Visual c ++

I assume you already have a basic knowledge of C ++ programming for Windows, if not, google it. The internet is full of lessons for beginners.

The first thing to do is add the serial port from the toolbar to the bottom form. This will allow you to change some important parameters of the serial port: port name, baud rate, bit rate. This is useful for adding controls to the application window, for changing these settings at any time, without recompiling the program. I only used the port selection option.

After searching for available serial ports, the first port is selected by default. How it's done:

array< String ^>^ serialPorts = nullptr;

serialPorts = serialPort1-> GetPortNames ();

this-> comboBox1-> Items-> AddRange (serialPorts);

this-> comboBox1-> SelectedIndex = 0;

The serial port on a PC can only be used by one application at a time, so the port must be open before use and not closed. Simple commands for this:

serialPort1-> Open ();

serialPort1-> Close ();

To correctly read data from the serial port, you must use events (in our case, an interrupt). Select the type of event:

Dropdown list when double clicking "DataReceived".

The event code is generated automatically:

If the first byte arriving on the serial port is 0xAB, if this means that the remaining bytes carry voltage data.

private: System :: Void serialPort1_DataReceived (System :: Object ^ sender, System :: IO :: Ports :: SerialDataReceivedEventArgs ^ e) (

unsigned char data0, data1;

if (serialPort1-> ReadByte () == 0xAB) (

data0 = serialPort1-> ReadByte ();

data1 = serialPort1-> ReadByte ();

voltage = Math :: Round ((float (data0 * 256 + data1) /1024*5.00), 2);

data_count ++;

serialPort1-> ReadByte ();

Writing and Reading Serial Port Data

A little problem for me was to send HEX RAW data over the serial port. The Write () command was used; but with three arguments: array, start byte number, number of bytes to write.

private: System :: Void button2_Click_1 (System :: Object ^ sender, System :: EventArgs ^ e) (

unsigned char channel = 0;

channel = this-> listBox1-> SelectedIndex;

array ^ start = (0xAC, (0x10 + channel));

array ^ stop = (0xAC, 0x00);

serialPort1-> Write (start, 0,2);

this-> button2-> Text = "Stop";

) else (

serialPort1-> Write (stop, 0.2);

this-> button2-> Text = "Start";

That's all!

Original article in English (translation: Alexander Kasyanov for cxem.net site)

Described how to assemble a homemade dual voltmeter based on the Arduino UNO platform using a 1602A LCD. In some cases, it is necessary to measure two DC voltages simultaneously and compare them. This may be required, for example, when repairing or adjusting a constant voltage regulator in order to measure the voltage at its input and output, or in other cases.

Schematic diagram

Using a universal microcontroller module ARDUINO UNO and a two-line LCD display type 1602A (based on the HD44780 controller), one can easily make such a device. On one line, it will show the voltage U1, on the other - the voltage U2.

Rice. 1. Schematic diagram of a dual voltmeter with 1602A display on Arduino UNO.

But first of all, I would like to remind you that ARDUINO UNO is a relatively inexpensive ready-made module - a small printed circuit board on which the ATMEGA328 microcontroller is located, as well as all its "strapping" necessary for its operation, including a USB programmer and a power supply.

For those who are unfamiliar with ARDUINO UNO, I advise you to first read articles L.1 and L.2. The dual voltmeter circuit is shown in Fig. 1. It is designed to measure two voltages from 0 to 100V (practically, up to 90V).

As can be seen from the diagram, a 1602A type H1 liquid crystal indicator module is connected to the digital ports D2-D7 of the ARDUINO UNO board. The LCD indicator is powered by a 5V voltage regulator on the 5V voltage regulator board.

The measured voltages are fed to two analog inputs A1 and A2. There are six analog inputs in total, - A0-A5, you could choose any two of them. In this case, A1 and A2 are selected. The voltage at the analog ports can only be positive and only in the range from zero to the microcontroller supply voltage, that is, nominally, up to 5V.

The analog port output is converted to digital form by the microcontroller's ADC. To get the result in units of volts, you need to multiply it by 5 (by the reference voltage, that is, by the supply voltage of the microcontroller) and divide by 1024.

In order to be able to measure a voltage of more than 5V, or rather, more than the supply voltage of the microcontroller, because the real voltage at the output of the 5-volt stabilizer on the ARDUINO UNO board may differ from 5V, and usually a little lower, you need to use ordinary resistive dividers at the input. Here these are voltage dividers across resistors R1, R3 and R2, R4.

At the same time, in order to bring the readings of the device to the real value of the input voltage, you need to set the division of the measurement result by the division factor of the resistive divider in the program. And the division factor, let's designate it "K", can be calculated by the following formula:

K = R3 / (R1 + R3) or K = R4 / (R2 + R4),

respectively for the different inputs of the dual voltmeter.

Interestingly enough, the resistors in the dividers do not have to be high-precision. You can take ordinary resistors, then measure their actual resistance with an accurate ohmmeter, and substitute these measured values ​​into the formula. You get the value "K" for a specific divisor, which will need to be substituted into the formula.

Voltmeter program

The C ++ program is shown in Figure 2.

Rice. 2. The source code of the program.

To control the LCD indicator, it was decided to use ports D2 to D7 of the ARDUINO UNO board. In principle, other ports are possible, but like this, I decided to use these.

In order for the indicator to interact with ARDUINO UNO, you need to load a subroutine into the program to control it. These routines are called "libraries" and there are many different "libraries" in the ARDUINO UNO software suite. The LiquidCrystal library is required to work with the HD44780 based LCD. Therefore, the program (table 1) starts by loading this library:

This line gives the command to load this library into ARDUINO UNO. Then, you need to assign the ARDUINO UNO ports that will work with the LCD indicator. I selected ports D2 through D7. Others can be selected. These ports are assigned by the line:

LiquidCrystal led (2, 3, 4, 5, 6, 7);

After that, the program proceeds to the actual operation of the voltmeter. To measure voltage, it was decided to use analog inputs A1 and A2. These inputs are given on lines:

int analogInput = 1;

int analogInput1 = 2;

The analogRead function is used to read data from analog ports. Reading data from analog ports occurs in the lines:

vout = analogRead (analogInput);

voutl = analogRead (analoglnput1);

Then, the actual voltage is calculated taking into account the division ratio of the input voltage divider:

volt = vout * 5.0 / 1024.0 / 0.048;

volt1 = vout1 * 5.0 / 1024.0 / 0.048;

In these lines, the number 5.0 is the voltage at the output of the stabilizer of the ARDUINO UNO board. Ideally, it should be 5V, but for accurate operation of the voltmeter, this voltage must first be measured. Connect the power supply and measure the + 5V voltage at the POWER connector of the board with a sufficiently accurate voltmeter. What will happen, then enter in these lines instead of 5.0, for example, if it is 4.85V, the lines will look like this:

volt = vout * 4.85 / 1024.0 / 0.048;

volt1 = vout1 * 4.85 / 1024.0 / 0.048;

At the next stage, you will need to measure the actual resistances of the resistors R1-R4 and determine the K coefficients (0.048 are indicated) for these lines using the formulas:

K1 = R3 / (R1 + R3) and K2 = R4 / (R2 + R4)

Let's say K1 = 0.046, and K2 = 0.051, so we write:

volt = vout * 4.85 / 1024.0 / 0.046;

volt1 = vout1 * 4.85 / 1024.0 / 0.051;

Thus, the program text needs to be changed according to the actual voltage at the output of the 5-volt stabilizer of the ARDUINO UNO board and according to the actual division factors of the resistive dividers. After that, the device will work accurately and will not require any adjustment or calibration.

By changing the division factors of the resistive dividers (and, accordingly, the "K" factors), you can make other measurement limits, and not necessarily the same for both inputs.

Karavkin V. RK-2017-01.

Literature:

  1. Karavkin V. - Christmas tree flasher on ARDUINO as a remedy for fear of microcontrollers. RK-11-2016.
  2. Karavkin V. - Frequency Counter on ARDUINO. RK-12-2016.

Initial data and revision

So at this point we have a DC voltmeter with a limit of 0..20V (see the previous part). Now we add an ammeter 0..5a to it. To do this, we slightly modify the circuit - it will become a pass-through, that is, it has both an input and an output.

I removed the part concerning the display on the LCD - it will not change. In principle, the main new element is the 0.1 ohm Rx shunt. The R1-C1-VD1 chain is used to protect the analog input. It makes sense to put the same at the input A0. Since we assume large enough currents, there are installation requirements - the power lines must be made with a sufficiently thick wire and connected to the shunt terminals directly (in other words, they are soldered), otherwise the readings will be far from reality. There is also a note on the current - in principle, the reference voltage of 1.1v allows you to register on the shunt 0.1 ohm current up to 11 amperes with an accuracy slightly worse than 0.01a, but when such a voltage drops on Rx, the power released will exceed 10 watts, which is not fun at all. To solve the problem, an amplifier with a gain of 11 on a high-quality op-amp and a 10 mΩ (0.01Ω) shunt could be used. But for now we will not complicate our life and will simply limit ourselves to a current of up to 5A (while the Rx power can be selected on the order of 3-5 W).

At this stage, a surprise awaited me - it turned out that the controller's ADC has a fairly large zero mixing - about -3mV. That is, the ADC simply does not see signals less than 3mV, and signals of a slightly higher level are visible with a characteristic inaccuracy of -3mV, which spoils the linearity at the beginning of the range. A quick search did not give any explicit references to such a problem (the zero offset is normal, but it should be significantly smaller), so it is quite possible this is a problem of a particular Atmega 328 instance. 0.06 volts), for current - a pull-up resistor on the 5V bus. The resistor is indicated by a dotted line.

Source

The full version of this volt-ampere meter (in the I2C version) can be downloaded from the link at the end of the article. Next, I'll show you the changes in the source code. Added reading of analog input A1 with the same averaging as for the voltmeter. In fact, this is the same voltmeter, only without a divider, and we get amperes according to Ohm's formula: I = U / Rx (for example, if the voltage drop across Rx = 0.01 V, then the current is 0.1A). I also introduced the current gain constant AmpMult - for the future. The AmpRx constant with the shunt resistance may have to be adjusted to account for the inaccuracy of the shunt resistor. Well, since this is already a volt-ampere meter and there is still room on the 1602 display, it remains to display the current power consumption in watts, having received not complicated additional functionality.

.... // Analog input #define PIN_VOLT A0 #define PIN_AMP A1 // Internal reference voltage (pick up) const float VRef = 1.10; // Ratio of the input resistive divider (Rh + Rl) / Rl. IN 0.2) InVolt + = 3; // Conversion to volts (In: 0..1023 -> (0..VRef) scaled by Mult) float Volt = InVolt * VoltMult * VRef / 1023; float Amp = InAmp * VRef / AmpMult / AmpRx / 1023; // To account for the fall on the shunt, uncomment 2 lines // float RxVolt = InAmp * VRef / 1023 / AmpMult; // Volt - = RxVolt; float Watt = Volt * Amp; // Outputting data lcd.setCursor (8, 0); lcd.print (Watt); lcd.print ("W"); lcd.setCursor (0, 1); lcd.print (Volt); lcd.print ("V"); lcd.setCursor (8, 1); lcd.print (Amp); lcd.print ("A"); )

Links

  • LiquidCrystal_I2C library for setting pinout

Hello, Habr! Today I want to continue the topic of "crossing" arduino and android. In a previous publication I talked about, and today we will talk about a DIY bluetooth voltmeter. Another such device can be called a smart voltmeter, "smart" voltmeter, or just a smart voltmeter, without quotes. The last name is incorrect from the point of view of the grammar of the Russian language, however, it is often found in the media. Voting on this topic will be at the end of the article, and I suggest starting with a demonstration of the device's operation in order to understand what the article will be about.


Disclaimer: the article is intended for the average arduino hobbyist who is usually not familiar with programming for android, therefore, as in the previous article, we will create an application for a smartphone using the App Inventor 2 visual development environment for android applications.
To make a DIY bluetooth voltmeter, we need to write two relatively independent programs: a sketch for arduino and an application for android. Let's start with a sketch.
To begin with, you should know that there are three main options for measuring voltage using an arduino, regardless of where you need to display the information: to the com-port, to the screen connected to the arduino, or to a smartphone.
First case: voltage measurements up to 5 volts. Here, one or two lines of code are enough, and the voltage is applied directly to pin A0:
int value = analogRead (0); // read readings from A0
voltage = (value / 1023.0) * 5; // only true if Vcc = 5.0 volts
Second case: A voltage divider is used to measure voltages over 5 volts. The scheme is very simple, the code is too.

Sketch

int analogInput = A0;
float val = 0.0;
float voltage = 0.0;
float R1 = 100000.0; // Battery Vin-> 100K -> A0
float R2 = 10000.0; // Battery Gnd -> Arduino Gnd and Arduino Gnd -> 10K -> A0
int value = 0;

Void setup () (
Serial.begin (9600);
pinMode (analogInput, INPUT);
}

Void loop () (
value = analogRead (analogInput);
val = (value * 4.7) / 1024.0;
voltage = val / (R2 / (R1 + R2));
Serial.println (voltage);
delay (500);
}


Arduino Uno
Bluetooth module
Third case. When you need to get more accurate information about the voltage, you need to use not the supply voltage as a reference voltage, which can change slightly when powered from a battery, for example, but the voltage of the Arduino's internal stabilizer of 1.1 volts. Here the circuit is the same, but the code is slightly longer. I will not analyze this option in detail, since it is already well described in the thematic articles, and the second method is quite enough for me, since I have a stable power supply from the laptop's USB port.
So we figured out the voltage measurement, now let's move on to the second half of the project: creating an android application. We will make the application directly from the browser in the visual development environment for android applications App Inventor 2. Go to the appinventor.mit.edu/explore website, log in with your Google account, click the create button, new project, and by simply dragging and dropping elements create something like this design:

I made the graphics very simple, if someone wants more interesting graphics, let me remind you that for this you need to use .png files with a transparent background instead of .jpeg files.
Now go to the Blocks tab and create the application logic there like this:


If everything worked out, you can click the Build button and save .apk to my computer, and then download and install the application on your smartphone, although there are other ways to upload the application. here it is more convenient to anyone. As a result, I got the following application:


I understand that very few people use the App Inventor 2 visual development environment for android applications in their projects, so there may be many questions about working in it. To remove some of these questions, I made a detailed video on how to make such an application "from scratch" (you need to go to YouTube to view it):

P.S. A collection of over 100 Arduino tutorials for beginners and pros

Top related articles