Raspberry Pi Measures 0~5V Voltage via Arduino

Raspberry Pi sadly doesn’t have any analog input, which means you could not connect an analog signal source to the GPIO pin on Raspberry Pi, and monitor its real-time output voltage in your application. Well, that’s not always true. You can wire your Pi with an external ADC (analog/digital converter), and measure the analog value via that. However that’s kind of complicated and expensive. Using a poor man’s A/D converter could save your money and time, but its accuracy is not that good.

Arduino usually has 10-bit ADC on board, and can measure voltage with resolution: 5V/1024=0.0049V, which is quite good in most scenarios. What’s more, many Arduino boards have USB port on board, which allows you to communicate with it via the serial port/USB. So Arduino seems to be a perfect extension for Raspberry Pi to accept analog input, right? We think so, but… every time you connect the Arduino to the Pi, its serial device name may vary. If your application running in Raspberry Pi wants to talk to the Arduino, it needs to know the Arduino’s serial device name. You definitely don’t want to manually find the correct device name and modify your application every time. That’s why we need the UUGear solution.

The UUGear solution contains a Sketch on Arduino side and a library for application that runs on Raspberry Pi. By using the library, your application on Raspberry Pi can control Arduino via a USB cable. Thus all those digital and analog pins on Arduino board are usable on Raspberry Pi. Interested? Please read on.

The Wiring

In this example, we will use a Raspberry Pi (with network connection), an Arduino Nano board, a multimeter, a DC 5V power supply and a potentiometer.

measure_voltage_wiringThe Raspberry Pi and the Arduino Nano are connected with the USB cable. We use the potentiometer to adjust output voltage from 0 to 5V. A multimeter is connected to keep monitoring the actual voltage. The output voltage is also connected to the A3 pin on Arduino Nano board.

The Source Code

A program (could be written in C or Python) can find the Arduino device by its unique, constant id, and then read the analog value from its A3 pin. Please read the project homepage to learn how to get the Arduino device id.

You will query the Arduino device by its id, and then call the analogRead() function to read the analog value. The actual voltage could then be calculated like this:

Voltage = value * 5 / 1024

Here is the source code written in C:

And here is the equivalent source code in Python:

When running the Python code, make sure the libUUGear.so and the UUGear.py files are placed in the working directory.

Video Demo

Here is a video that demonstrate the result, which is really not bad:

 Conclusion

Thanks to UUGear solution, the application running on Raspberry Pi can control the connected Arduino via an abstract model. Then thanks to Arduino’s capability to accept analog input, Raspberry Pi + Arduino could be used as a decent digital voltmeter, or even a multimeter if you are willing to add more circuits. This idea could be used in many scenarios, and we are sure there will be a lot of fun on finding them out.

See also