• sales

    +86-0755-88291180

RPi Tutorial Series: PCF8591 AD/DA User Guide

Raspberry Pi doesn't embed any AD/DA converters, so if you want to use analog sensors on Raspberry Pi, an external AD expansion baord is required.

An AD/DA converter chip, PCF8591, is embedded within Pioneer600 expansion board, which may be helpful for AD sampling on Raspberry Pi. The PCF8591 features a 8-bit A/D converter and 4 D/A converters. Controlled by I2C interface.

Some examples are given in this section. You should install some specific libraries before trying these examples, see: Libraries Installation for RPi.

DAC

C program based on BCM library

#include <bcm2835.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    char Buf[]={0, 0};
    unsigned char value=0;

    if (!bcm2835_init()) return 1;
    bcm2835_i2c_begin();
    // The I2C address of PCF8591 is 0x48.
    bcm2835_i2c_setSlaveAddress(0x48);
    bcm2835_i2c_set_baudrate(10000);
    printf("start..........\n");

    while(1)
    {
        Buf[0] = 0x40;
        Buf[1] = value++;
        bcm2835_i2c_write(Buf, 2);
        printf("AOUT: %d\n", value);

        bcm2835_delay(20);
    }

    bcm2835_i2c_end();
    bcm2835_close();
    return 0;
}

Save the file as "pcf8591.c". Compile and run with:

gcc -Wall pcf8591.c -o pcf8591 -lbcm2835
sudo ./pcf8591

Python program based on smbus library

#!/usr/bin/python
# -*- coding:utf-8 -*-
import smbus
import time

address = 0x48
cmd = 0x40
value = 0

bus = smbus.SMBus(1)
while True:
    bus.write_byte_data(address,cmd,value)
    value += 1
    if value == 256:
        value =0
    print("AOUT:%3d" %value)
    time.sleep(0.01)

Save the file as "pcf8591.py" and execute it with:

sudo python pcf8591.py

C program based on WiringPi

#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>

#define Address 0x48
#define BASE 64
#define A0 BASE+0
#define A1 BASE+1
#define A2 BASE+2
#define A3 BASE+3

int main(void)
{
    unsigned char value;
    wiringPiSetup();
    pcf8591Setup(BASE,Address);

    while(1)
    {
        analogWrite(A0,value);
        printf("AOUT: %d\n",value++);
        delay(20);
    }
}

Save the file as "pcf8591.c". Compile and run with:

gcc -Wall pcf8591.c -o pcf8591 -lbcm2835 -lwiringPi
sudo ./pcf8591

ADC

C program based on BCM library

#include <bcm2835.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    char Buf[]={0};
    unsigned char i;

    if (!bcm2835_init())return 1;
    bcm2835_i2c_begin();
    bcm2835_i2c_setSlaveAddress(0x48);
    bcm2835_i2c_set_baudrate(10000);
    printf("start..........\n");

    while(1)
    {
        for(i = 0;i < 4;i++)
        {
            Buf[0] = i;
            bcm2835_i2c_write_read_rs(Buf,1,Buf,1);
            bcm2835_i2c_read (Buf,1);
            printf("AIN%d:%5.2f  ",i,(double)Buf[0]*3.3/255);
        }
        printf("\n");
        bcm2835_delay(100);
    }

    bcm2835_i2c_end();
    bcm2835_close();
    return 0;
}

Save the file as "pcf8591.c". Compile and run with:

gcc -Wall pcf8591.c -o pcf8591 -lbcm2835
sudo ./pcf8591

Python program based on smbus library

#!/usr/bin/python
# -*- coding:utf-8 -*-
import smbus
import time

address = 0x48
A0 = 0x40
A1 = 0x41
A2 = 0x42
A3 = 0x43
bus = smbus.SMBus(1)
while True:
    bus.write_byte(address,A0)
    value = bus.read_byte(address)
    print("AOUT:%1.3f  " %(value*3.3/255))
    time.sleep(0.1)

Save the file as "pcf8591.py" and execute it with:

sudo python pcf8591.py

C program based on WiringPi

#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>

#define Address 0x48
#define BASE 64
#define A0 BASE+0
#define A1 BASE+1
#define A2 BASE+2
#define A3 BASE+3

int main(void)
{
    int value;
    wiringPiSetup();
    pcf8591Setup(BASE,Address);

    while(1)
    {
        value = analogRead(A0);
        printf("Analoge: %dmv\n",value*3300/255);
        delay(1000);
    }
}

Save the file as "pcf8591.c". Compile and run with:

gcc -Wall pcf8591.c -o pcf8591 -lbcm2835 -lwiringPi
sudo ./pcf8591
TAG: Raspberry Pi CM5 Official Original IO Board a Base Development Platform Compute Module 5 IO Board User Guide Raspberry Pi Pico 2 RP2350B 2.8 inch LCD Development Board RGB Display Round TouchScreen 480x480 LVGL QMI8658 / SD / RTC raw display Sipeed NanoCluster Mini Cluster Board mini data center For Raspberry Pi CM45 / Computer /LM3H /M4N DeepSeek XiaoZhi AI Chat Robot ESP32-S3 1.54 inch LCD N16R8 MUMA Development Board Display Screen Wifi Weather Station TimeClock Raspberry Pi 5 PCIe to SATA Expansion Board with EEPROM HAT+ Omni-Directional Lidar 3.52 inch e-Paper HAT B E Ink Red/Black/White 360x240 Screen Display For Arduino /Raspberry Pi /Jetson Nano /STM32 ESP32 C6 Development Board 1.47 inch LCD Touch Screen 1.47inch Display 172&times;320SD AXS5106L JD9853 Raspberry Pi 5 Sound UNO Minima RA4M1 ZERO Tiny SuperMini R7FA4M1 Board Compatible With R4 For Arduino Raspberry Pi CM5 Compute Module 5 Development Kit With CM5104032 + Official Original IO Board + Case Jetson Nano Arduino Thermal imaging camera Raspberry Pi Pico ST7789 Milk-V Duo Raspberry Pi 5 inch LCD 720x1280 Capacitive TouchScreen Display HDMI For Jetson Nano/mini Computer PC USB Camera Raspberry Pi 5 Silver-Shadow Case