Skip to content

PCF8591 AtoD and the voltage meter

Mark Niemann-Ross edited this page Jan 6, 2017 · 11 revisions

The voltage meter on the front panel doesn't actually mean what you might think. The meter doesn't show any meaningful voltage - instead, it's controlled by the Raspberry Pi and can indicate anything it's programmed to show.

<img src="https://github.com/mnr/rubberfish/wiki/images/voltageMeter.jpg" width=300px">

The connection between RPi and the voltage meter is a PCF8591 Analog to Digital module and a transistor.

I'm using a uxcell PCF8591 AD DA Converter Module. The PCF8591 refers to the chip itself, rather than the module. There are lots of pre-mounted versions available. All of these boards appear to be set up for sensor experimentation and all have three sensors that can be read (thermistor, photo resistor, and potentiometer). They also have one Digital to Analog converter (DAC) pin going out. The modules all support I2C, however they all appear to pre-fixed at address 0x48. I paid less than $10 for this board.

<img src="https://github.com/mnr/rubberfish/wiki/images/pcf8591.jpg" width=300px">

To drive the voltage meter, I originally connected the Aout/DAC directly to the voltage meter. Since I am feeding the board (Vcc) 3.3 vdc, the voltage meter showed between zero and three volts. I was hoping for a wider range.

To get a wider range, I used a small NPN transistor to boost the signal. Using the schematic below, I feed 12 vdc to Vcc and the Aout/DAC signal to the input (base) of the transistor. The output signal then goes to the voltage meter. I now get a display range between 3 vdc and 11 vdc (roughly).

<img src="https://github.com/mnr/rubberfish/wiki/images/transistorGain.jpg" width=300px">

(My gratitude to Backyard Brains )

I'm considering changing the display in the voltage meter to something like "Extrovert ... Introvert" or an emotional scale.

How to set up the Raspberry Pi to drive the PCF8591

Connecting to the PCF8591 involves:

  • enabling I2C on the Raspberry Pi
  • Connecting the GPIO I2C pins to the PCF8591 (described above)
  • Check to see that I2C is working (use i2c-tools as described below and at [John's Linux Blog](1. control code for the PCF8591))
  • Create some code to drive the PCF8591

install i2ctools

If you don't already have i2ctools, you'll want to install them.

apt-get update

apt-get install i2c-tools

use i2cdetect to check that the module is online

i2cdetect -y 1

You should see the board appear at 0x48

use i2cget and i2cset to test values

i2cget -y 1 0x48 will read Ain1

i2cget -y 2 0x48 will read Ain2

i2cget -y 3 0x48 will read Ain3

i2cset -y 1 0x48 0x41 0xff will send a voltage out of Aout/DAC. Change the last byte from 0x00 to 0xFF to change the voltage