KS0213 Keyestudio RPI GPIO-PCF8591 Shield

From Keyestudio Wiki
Jump to navigation Jump to search



Introduction

There's no AD/DA function on the Raspberry Pi GPIO interface, this may trouble you in the Pi development. However, it won't be a problem anymore. The RPI GPIO-PCF8591 Shield allows you to add high-precision AD/DA functions to the Raspberry Pi.
The PCF8591 features four-8-bit analog to digital converters and a single 8-bit digital to analog converter. This will operate through the I2C interface on the Raspberry Pi.

thumb


Connection Diagram

Plug it directly into Raspberry Pi to start your works as shown in below figure.

thumb


Sample Code

DA(Digital to Analog) Source Code:
#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>

#define Address 0x48
#define BASE 64          //DA Output
#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);    //configuration pcf8591	

	while(1)
	{		
               analogWrite(BASE,value);      //write the value output on AOUT interface
               printf("AOUT:%d\n",value);     //print that value
               delay(50);	
	}
}

AD(Analog to Digital) Source Code:
#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>

#define Address 0x48         //pcf8591 address 
#define BASE 64
#define A0 BASE+0           //A0 input
#define A1 BASE+1           //A1 input
#define A2 BASE+2           //A2 input
#define A3 BASE+3           //A3 input
int main(void)
{
    unsigned char value;
	wiringPiSetup();
	pcf8591Setup(BASE,Address);        //configurationpcf8591
	
	while(1)
	{
               value=analogRead(A0);     // read A0 value          
               printf("A0:%d\n",value);  // print A0 value
               delay(100);	
	}
}


Program Writing


1.Install Wiringpi Library

Wiring Pi is a library. WiringPi supports analog reading and writing, and while there is no native analog hardware on a Pi by default, modules are provided so other A/D and D/A devices can be implemented relatively easily.
We will provide a Wiringpi installation package or click on this URL: https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/
(it should open in a new page) to download the latest version. This will download a .tar file with a name like wiringPi-b0a60c3.tar. Then copy the file and put it into the raspberry pi file as shown in below figure:

thumb

Type in this command to unzip the source code and install:

  • tar xfz wiringPi-98bcb20.tar.gz
  • cd wiringPi-98bcb20
  • ./build

Note that the actual file name will be different (may not be 98bcb20) – you will have to check the name and adjust accordingly.
Here is the interface after installing WiringPi as shown in below figure.

thumb

WirtingPi includes a set of gpio commands to control the GPIO pins of the Raspberry Pi. Type in the command to check the installation:

  • gpio -v
  • gpio readall

The interface as shown in below figure means complete installation.

thumb


2.Enable I2C Utility

The I2C bus allows multiple devices to be connected to your Raspberry Pi. Before using I2C it needs to be configured.
Run this command: sudo raspi-config and follow the prompts to install I2C. Now complete the following steps:

  • Select “9 Advanced Options”
  • Select “A6 I2C”

The screen will ask if you want the ARM I2C interface to be enabled :

  • Select “Yes”
  • Select “Ok”
  • Select “Finish” to return to the command line

When you next reboot the I2C module will be loaded.


3.Programming

Copy the file pcf8591 provided by us and put it into your pi directory through winSCP. Next, type this command: cd pcf8591 to go inside the pcf8591 folder. Then type this command: make to make an executable file. This means this is the file we run to launch the program.
Finally type this: sudo ./TTP229 to launch the program.


Result

DA source code makes AOUT interface output 0-5V voltage; the pi will prints digital value among 0-255 on the terminal after reading out voltage set by AD source code.



Documents

https://fs.keyestudio.com/KS0213


Buy From