- The i2cdetect command is used to detect the I2C devices (the --y option skips any warnings about possible interference with other hardware that could be connected to the I2C bus). The following commands are used to scan both the buses:
sudo i2cdetect -y 0 sudo i2cdetect -y 1
- Depending on your Raspberry Pi board revision, the address of the device should be listed on bus 0 (for Model B Rev1 boards) or bus 1 (for Raspberry Pi 2 and 3, and Raspberry Pi 1 Model A and Model B Revision 2). By default, the PCF8591 address is 0x48:
I2C bus number to use |
Bus 00 |
Bus 11 |
Raspberry Pi 2 and 3 |
HAT ID (I2C_VC) |
GPIO (I2C_ARM) |
Model A and Model B Revision 2 |
P5 |
GPIO |
Model B Revision 1 |
GPIO |
N/A |
- The following screenshot shows the output of i2cdetect:
The PCF8591 address (48) is displayed here on bus 1
If nothing is listed, shut down and double-check your connections (the ADC module from www.dx.com will switch on a red LED when powered).
If you receive an error stating that the /dev/i2c1 bus doesn't exist, you can perform the following checks:
- Ensure that the /etc/modprobe.d/raspi-blacklist.conf file is empty (that is, that the modules haven't been blacklisted), using the following command to view the file:
sudo nano /etc/modprobe.d/raspi-blacklist.conf
- If there is anything in the file (such as blacklist i2c-bcm2708), remove it and save
- Check /boot/config and ensure there isn't a line that contains device_tree_param= (this will disable support for the new device tree configurations and disable support for some Raspberry Pi HAT add-on boards)
- Check whether the modules have been loaded by using lsmod and look for i2c-bcm2708 and i2c_dev
- Using the detected bus number (0 or 1) and the device address (0x48), use i2cget to read from the device (after a power up or channel change, you will need to read the device twice to see the latest value), as follows:
sudo i2cget -y 1 0x48 sudo i2cget -y 1 0x48
- To read from channel 1 (this is the temperature sensor on the module), we can use i2cset to write 0x01 to the PCF8591 control register. Again, use two reads to get a new sample from channel 1, as follows:
sudo i2cset -y 1 0x48 0x01 sudo i2cget -y 1 0x48 sudo i2cget -y 1 0x48
- To cycle through each of the input channels, use i2cset to set the control register to 0x04, as follows:
sudo i2cset -y 1 0x48 0x04
- We can also control the AOUT pin using the following command to set it fully on (lighting up the LED D1):
sudo i2cset -y 1 0x48 0x40 0xff
- Finally, we can use the following command to set it fully off (switching off the LED D1):
sudo i2cset -y 1 0x48 0x40 0x00