question

laurenceh avatar image
laurenceh asked

How do I access the unused analogue input on the raspberry pi (Safely)

How do I safely read the spare analogue inputs on a raspberry pi that are not being used by Venus OS without impacting what Veus OS is trying to do with the interface?


As I understand it the MCP3208 has 8 analogue inputs and Venus OS assigns 5 of them like this.
ADC[0]: 6
ADC[1]: 5
ADC[2]: 1999 (tank3)
ADC[3]: 1999 (temp2)
ADC[4]: 1997 (tank1)
ADC[5]: 1998 (temp1)
ADC[6]: 1996 (tank2)
ADC[7]: 4 (does not exist on beaglebone)


Which means there are three unused analogue inputs which I would like to access for things such as a humidity sensor or more temperatures (fridge and cabin). I am not thinking of integrating these with Venus OS at the moment as once I have the data I can make it visible in other ways.

I know there are programs and scripts or python available which drive the GPIO pins to interface to the MCP3208. But I don't want to just dive in and start trying to read the unused inputs with sample code I find online while Venus OS is active and presumably talking to the analogue interface on a regular basis.

Where should I start ?

Raspberry PiTemperature Sensortank monitor
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

3 Answers
laurenceh avatar image
laurenceh answered ·

Having done the research and sorted this out I am posting my own answer for others.

This is has been tested on Rpi3B+ with Venus OS 2.42.

The steps for getting the Raspberry pi analogue inputs are documented in the following posts.

https://community.victronenergy.com/questions/40130/where-do-i-find-dbus-adc-files-for-raspberry-pi-in.html

and

https://community.victronenergy.com/articles/38710/victron-raspi-hat.html

Once these steps are complete and the analogue inputs are working with Venus OS it appears the raw data for all 8 inputs is available in the /sys/bus/iio devices like this.

root@raspberrypi2:# cd /sys/bus/iio/devices/iio\:device0/

root@raspberrypi2:/sys/bus/iio/devices/iio:device0# ls

dev in_voltage2-voltage3_raw in_voltage5-voltage4_raw in_voltage_scale

in_voltage-voltage_scale in_voltage2_raw in_voltage5_raw name

in_voltage0-voltage1_raw in_voltage3-voltage2_raw in_voltage6-voltage7_raw of_node

in_voltage0_raw in_voltage3_raw in_voltage6_raw power

in_voltage1-voltage0_raw in_voltage4-voltage5_raw in_voltage7-voltage6_raw subsystem

in_voltage1_raw in_voltage4_raw in_voltage7_raw uevent


The value on any interface can be read directly from the device psuedo files like this:

root@raspberrypi2:/sys/bus/iio/devices/iio:device0# cat in_voltage5_raw


3197


You may find reference to a script read_adc.sh in other threads, this script needs to be created on the Venus OS Rpi. Probably best to put it in a test directory on the /data partition.


root@raspberrypi2:/data/test# nano read_adc.sh

enter the following text:


for i in 0 1 2 3 4 5 6 7; do

echo -n "ADC[${i}]: "

cat /sys/bus/iio/devices/iio\:device0/in_voltage${i}_raw


done


Then set execute for the file.

root@raspberrypi2:/data/test# chmod 775 read_adc.sh


root@raspberrypi2:/data/test# ./read_adc.sh

ADC[0]: 3178

ADC[1]: 3183

ADC[2]: 3309

ADC[3]: 3192

ADC[4]: 3324

ADC[5]: 3197

ADC[6]: 3298

ADC[7]: 3177







2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

laurenceh avatar image
laurenceh answered ·

In case anyone else find this entry when looking for answers I am updating this as of July 2022.

From Venus 2.8 onwards the extra analogue interfaces can be enabled in the console (and VRM) by editing the file /etc/venus/dbus-adc.conf.

To enable the additional interface they should be added to the file so that it looks like this:

device  iio:device0
vref    1.8
scale   4095
temp    0
temp    1
tank    2
temp    3
tank    4
temp    5
tank    6
temp    7

The vref and scale should be left unchanged for your system.

It is of course necessary that the circuits you have connected to the analogue inputs are the same as used by victron to drive their default tank and temperature sensors for this to work.

The additional channels are 0, 1 and 7

The additional interfaces can then be configured in settings->IO/Analogue Inputs

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

johanndo avatar image
johanndo answered ·

The raspberry pi 4has no analog inputs, you need an external ADC.

1 comment
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

jeroend avatar image jeroend commented ·

Correct, neither has the Raspberry Pi 3 hence why the chip MCP3208 is mentioned in the opening post. That will add the 8 analog ports to the Raspberry Pi mentioned here.

This chip alone will not be enough to see eg. measured temperature values but with a few additional resistors and capacitors it can be achieved. See the articles mentioned by LaurenceH.

0 Likes 0 ·