Skip to content

Implementing ADC with photocell

milanjelisavcic edited this page Aug 3, 2016 · 1 revision

Implementing ADC with photocell

ADC for Raspberry Pi is based on the PCF8591 Analog to Digital Converter (ADC). This chip has 4 analog inputs (ADC) and one analog output or Digital to Analog Converter (DAC). The first step is to physically hook up the board. The cables must be connected to the Raspberry Pi GPIO pins nominated for I2C. These have the required 'pull up resistors' already installed. (These are what make the wires operate like a bus). The pins are:

  • P1-01 +3.3v (VCC)
  • P1-03 Data (SDA)
  • P1-05 Clock (SCL)
  • P1-09 Ground (GND)

I2C Installation for Raspberry Pi

The new Raspbian distro already have the I2C driver installed but they are disabled by default. To enable it all you need to do is comment out a line by putting # in front. At the prompt type.

sudo nano /etc/modprobe.d/raspi-blacklist.conf

Next edit the modules file by adding @i2c-dev@ and @i2c-bcm2708@ to a new line:

sudo nano /etc/modules

enable I2C and reboot

Now install the i2c-tools package by:

sudo apt-get update
sudo apt-get install i2c-tools python-smbus
sudo adduser pi i2c

Enabling I2C and follow the prompts to install i2c support for the ARM core and Linux kernel

sudo raspi-config
sudo shutdown -r now

The next test is to see if the i2c driver can talk to chip. The Raspberry Pi actually comes configured with two I2C buses and on newer systems the bus labelled I2C0 is allocated the Linux device i2c-1.

i2cdetect 1

WARNING! This program can confuse your I2C bus, cause data loss and worse! I will probe file /dev/i2c-1. I will probe address range 0x03-0x77. Continue? [Y/n] y 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --

You can see here that a device has been detected at address 0x48.

Reading and writing to the chip is quite straight forward but the chip does have a few nuances. The first read after power on will return 0x80. The analog to digital conversion is performed when you make a read request but the read will return the previous sample so it is always one sample behind. This is not too confusing unless you are switching to read a different input.

All these commands take a parameter 1 to specify which i2c bus and I pass -y which skips the safety warning. (We know what we are doing so this is OK. On other hardware such as your PC, things can and do go wrong by using these commands).

The most basic read is the default channel (input 0).

i2cget -y 1 0x48

0x80

That is the power on status code. Now we read again

i2cget -y 1 0x48

0xd2

At end, install the python3-smbus python module:

sudo apt-get install python3-smbus
_________________
/ Premature      \
| optimization   |
| is the root of |
| all evil.      |
|                |
\ -- D.E. Knuth  /
-----------------
    \   ^__^
     \  (oo)\_______
        (__)\       )\/\
            ||----w |
            ||     ||
Clone this wiki locally