Reading an Analogue Voltage with Arduino over USB Serial

 

Arduino nano connected to voltage divider

The purpose of this mini project is to check that we can read the analogue signal from the voltage divider that was made previously. Transfer the value via serial connection and perform a calculation to check against measured values.

 

The circuit diagram is as follows :


 

To start, the voltage divider is connected to a 5v supply and analogue pin A0 of an Arduino Nano 3.

In the Arduino IDE the code is as follows :

const int pinA0 = A0;      // Analogue input pin that the battery is attached to
int vBattery = 0;            // This will be our voltage reading.
int incomingByte = 0;   // Holds the request

void setup() {
  Serial.begin(9600);
                    // Start serial communications
  Serial.println("Request reading by sending a request");
  Serial.println("Pin A0, Battery = b");
}

void loop() {
  vBattery = analogRead(pinA0);     
// read the analogue value:
  if (Serial.available() > 0) {            // Is there a request in the buffer
    incomingByte = Serial.read();       //Reads value
    if (incomingByte == 98) {            //98 is ASCII lowercase b
      Serial.println(vBattery);            //Sends the value over serial
    }
  }
}

We start by declaring some variables, starting a serial connection, sending some brief instructions and then waiting to receive a value. If the value is 98 ASCI equivalent "b" then we send the reading back over the USB serial connection.


Serial connection is up, lets send a "b" to request the value.

We received value 351
Multimeter value of divider R2 = 1.56v
Multimeter supply voltage to divider = 4.96v
Calculate divider multiplier 4.94 / 1.56 = 3.166

Our calculation to convert the value to voltage is:

Voltage = Max value of input / Max value of Arduino sensor * received value
V = 5 / 1024 * 351
V = 1.714
Difference of 0.154v from measured before multiplying.
1.714 * 3.166 = 5.426
Difference of 5.426 - 4.96 = 0.466
I do not think this is accurate enough, we should be able to do better !


Lets check some values for accuracy.

Readings taken without the voltage divider.

5v Supply connected to A0
Actual 4.97v
We receive 1023
V = 5 / 1024 * 1023
V = 4.995
Difference 0.025v

3.3v Supply connected to A0
Actual 3.36v
We receive 725
V = 5 / 1024 * 725
V = 3.540
Difference 0.18v

The readings all seem slightly off even without the voltage divider. After a lot of searching the internet it turns out that the Arduino analogue reading is only as accurate as its 5v reference. The voltage measured at this pin is only 4.61v :/

We could connect an accurate 5v reference voltage to this pin such as a Texas Instruments LM4040 for around £1.50 but I do not have one available at present.

Another way is to measure the 5v ref and adjust the calculation.

Using the Arduino measured voltage at 5v output 4.61v we repeat the calculation.

We received value 351
Multimeter value of divider R2 = 1.56v
Multimeter supply voltage to divider = 4.96v
Calculate divider multiplier 4.94 / 1.56 = 3.166

Voltage = Max value of input / Max value of Arduino sensor * received value
V = 4.61 / 1024 * 351
V = 1.580
Difference of 0.02v from measured before multiplying.
1.580 * 3.166 = 5.002
Difference of 5.002 - 4.96 = 0.042v

This is a massive improvement from our first reading that was out by 0.466v ! Unfortunately it requires measuring the 5v ref if the power supply changes and I will be running this off a 12v solar powered battery circuit with a poor 5v USB output. For this reason the correction code will be implemented external of the Arduino so that it can be easily changed. I will also purchase some 5v references and update with my experiences.

This took a lot longer than I thought it would, I hope it helps anyone else trying to do something similar.



Very good source for Arduino experiments : http://www.skillbank.co.uk/arduino/measure.htm

Calibrating voltage divider :  https://startingelectronics.org/articles/arduino/measuring-voltage-with-arduino/

Comments

Popular posts from this blog

Intro and Welcome

How to stop Outlook Meeting Updates Going Directly to Deleted Items

ScreenGrab