Welcome to the latest entry of the Programming with Arduino and Protocoder for makers course! In this entry we will be using our Arduino board and a couple of cables to create a voltmeter. It´s easy, very quick and although there are some limitations with conventional voltmeters, it is useful. We will also use this code again in another tutorial to create a voltmeter with interface on a mobile phone or tablet.
List of materials
- ZUM BT-328 controller board or one compatible with Arduino
- Two lengths of cable 15 cm long or cable jumper
Electrical connections
This time, we will connect the black cable to the Arduino GND pin and the other to an analogue input, in this case A0. You can see the connection in the image below:
Limitations
We can only read positive values from 0 to 5 volts.
The code
As always, we will declare the global variables of the program:
1 2 |
int pinSonda = A0; float escala = 100; //100 for volts, 0.1 for millivolts |
Here only one cable is connected to the A0 analogue input, which we will use in probe mode, meaning that we will use this cable to measure the voltage. We will also create the scale variable, which we can modify to get the reading in volts if we make the variable equal to 100, or in millivolts if we make it equal to 0.1, we will see why we have to apply these values later. Then we start the function in the setup function:
1 2 3 4 5 6 7 |
void setup(){ Serial.begin(19200); pinMode(pinSonda, INPUT); } |
This time we start the communication at 19,200 bauds/second which is the communication speed of the Bluetooth module included with the ZUM BT-328 boards, as we will use the same example to create a voltmeter in Protocoder later. We also mark the INPUT pin we have connected to the cable that will act as the probe. Then, in the loop function:
1 2 3 4 5 6 7 8 9 10 11 |
void loop(){ float lectura = analogRead(pinSonda); lectura = map(lectura, 0, 1023, 0, 500); float voltaje = lectura/escala; Serial.println(voltaje); delay(500); } |
First we create the read variable and we store the read value in the pinSonda using the analogRead function and using the map function, we transform the value of the analogue reading, from 0 to 1023 until 0 to 500. Then we create the voltage float type variable to which we will apply the scale factor. If we divide by 100, we will obtain the value of 0 to 5 volts, but if we divide by 0.1, we will increase the scale and obtain the value in millivolts. Depending on the type of measurement we want to take, it could be interesting to use one scale or another. Finally, we apply a small delay of 0.5 seconds so that the data can be read on screen. As an exercise, have a go at modifying the program to display the average voltage measured during a set period of time. See you again!!