Chapter 8 - ADC Expander

The PCF8591 ADC / DAC converter modules shown here are available for about a quid.
They comprise:

a.  8-bit DAC (digital to analog converter)
b.  four 8-bit ADC (analog to digital converters).
c.  on-board thermistor, LDR (light dependent resistor), and POT (potentiometer).

These last 3 items are for analog experimentation, and can be disconnected by removing jumpers.
So note that the 3 jumper links are not for addressing, which is fixed at 72 (decimal).
NOTE: despite any apparent similarity of boards, the left-hand (i2c) connectors are very different.
The VCC's and GND's are on opposite sides, and even the SDA and SCL pins are transposed.

TIP: the bottom module has the correct pinout to plug directly into the blue PCF8574 modules.
Thus offering 4 x ADC + 1 DAC + 8 GPIO's from 2 modules plugged into the 2-wire i2c bus.

NOTE: the reason for choosing to use an i2c device is because it only requires 2 gpio pins to be available, so therefore can even be used withSonoffs etc by utilising their TX and RX gpios.

Using these modules is quite easy once you are aware of how they are accessed on the i2c bus.
Writing data to the DAC is just a matter of opening a channel to that device address, send an instruction to explain the transaction requirements, then write the expected data, and close the channel again afterwards.

Each i2c device responds to a specific address on the i2c bus, which is hardwired to 72 (decimal) on these PCF8591 modules.
So let us first define an easy constant to address them with...
PCF8591_address = 72

To send or receive any data to any i2c address requires waking up that address using...
 i2c.begin PCF8591_address
Now send an instruction to that waiting address...
i2c.write 64                              'this tells the PCF9591 to use the next incoming byte of data to set the value of the DAC

Now send the expected byte of data to set the DAC value (V min = 00, V mid = 127, V max = 255)...
 i2c.write V

After transaction is completed, close the connection to that address.

Reading data is similar - open a channel to the address, write an instruction to request the required read transaction, notify how many bytes to read (which can vary according to requirements), then read them - this process is demonstrated in the upcoming analog joystick project..

For now, let's complete the PCF8591 DAC overview using a simple script to send an incrementing number to the DAC then similarly send a decrementing number, and repeat it 5 times so we can see something happening.
There should be an LED connected to the DAC output, which will brighten and dim as the DAC output voltage rises and falls.
This is handy for demonstration purposes, but be aware that the LED will 'load' the DAC output and cause inaccurate results (cured by a snip).

'                                          PCF8591 DAC
'i2c.setup 4, 5                     'i2c RX and TX pins need to be configured as appropriate
i2c.setup 5, 4                     'i2c RX and TX pins need to be configured as appropriate
PCF8591_address = 72    'set to module i2c address
for a = 1 to 5
 wlog str$(a)
 for v = 0 to 255
 i2c.begin PCF8591_address
 i2c.write 64
 i2c.write v
 next v
 for v = 0 to 255
 i2c.begin PCF8591_address
 i2c.write 64
 i2c.write 255 - v
 next v
next a
wlog "finished"
'----------- End ----------

Margaret Baker,
Feb 10, 2018, 6:43 AM