February 15, 2021

Raspberry Pi measuring Voltage

I'm always a little confused that as soon as someone wants to get an analog measure to a Raspberry Pi(RPi) they are often taking a detour via an Arduino/ESP or some other microcontroller. 

Why not just take an Arduino peripheral and use it together with a RPi ? 

In my case I needed to measure the voltage across a fresh water tank level sensor in our boat, but i could be any DC voltage. Hints for sending the data to a Signal K(SK) server is found below. 

In this post I will use an ADS 1115(AD), or ADS 1015, as AD converter.

Intro
In a parallell project, the Victron SmartShunt, I needed to send some serial input data as UDP, to a remote SK server, and therefore this setup is done with a Raspberry Pi Zero W(RPiZ) serving both purposes.

Prerequisites
I'm no "coder" so the setup is using NodeRed installed on the RPiZ.
So install; 
The install script for NR, mentioned in the NR post, takes care of both the standard RPi and the RPiZ despite it's different hardware. 

Python
If you like coding there are great tutorials and libraries found at the Adafruit pages.

Setup
The AD uses the I2C bus for communication which you enable via "ssh" and 

sudo raspi-config

Interface Options
    P5  I2C  Enable/disable automatic loading of I2C kernel module

Power down the RPiZ and connect the pins to the AD according to

 RPiZ pin       ADS 1X15 pin
 3, SDA   SDA
 4, +5VDC     VDD
 5, SCL      SCL
 6, Ground      GND

and then boot the RPiZ and check via "ssh" and

ls /dev/i2*

and probably "/dev/i2c-1" will appear.

Now you can go on and test with NodeRed but there are good testing tools installed with 

sudo apt install i2c-tools

check with 

i2cdetect -l

which will show the active bus. Go on with

sudo i2cdetect -y 1


and the AD will show up with its default adress hex 48.

Getting the data
Access NodeRed and install "node-red-contrib-anolog-to-digital-converter-raspberry-pi" and set it up according to the good documentation. 

Please note that the mentioned AD node is replaced by "node-red-contrib-ads1x15_i2c" so the flow below has to be updated !!

You could use up to 4 inputs but in my case I used the differential input which limits to 2 inputs but can show both negative and positive values. (Very nice for sensing current through a shunt and if the current is going out or in to the batteries.) Full documentation is found at the Adafruit WEB-site.

Voltage divider
As said in the documentation you can't measure DC voltages bigger than the the supply voltage, VDD, 5VDC. To solve this you use a voltage divider.

SK Delta
The measured voltage, in my case, is used to source my Signal K(SK) server and can even be sent out, with proper hardware, on to the NMEA 2000(N2K) network to be displayed on any N2K compatible instrument. Similar if using NMEA 0183.

In a NR function Node I'm formatting the output, for testing purposes, to a SK delta and then sending it on via WEB-socket to the SK server.

The "/dev/ttyS0" is a node to handle the serial output from the Victron shunt and then send the data to the target SK server with UDP.

Before importing the flow you will also need to install "node node-red-node-serialport".

2 comments:

  1. Just a thought, would a Raspberry Pi Pico with built in a-d converters be a neat solution - possibly even cheaper than using an add-on Arduino board?

    ReplyDelete
    Replies
    1. Absolutely, if its OK with 12 bit ADC and you like Python/C++. I really like NodeRed, JS and are trying to get better in Linux not adding another environment. Thanks for your comment.

      Delete

Feel free to leave a comment ! ... but due to a lot of spam comments I have to moderate them. Will reply ASAP !