6. Chapter ADC Module

In this chapter we will learn how to use an ADC module.

6.1. Project Voltmeter

This project uses an ADC module to read potentiometer voltage value and display the value on Display Window.

6.1.1. Component List

  1. Raspberry Pi (with 40 GPIO) x1

  2. GPIO Extension Board & Ribbon Cable x1

  3. Breadboard x1

Rotary potentiometer x1

Rotary-potentiometer

Resistor 10kΩ x2

Resistor-10kΩ

ADC module x1

ADC-module-1 or ADC-module-2

Jumper Wire x16

jumper-wire

This product contains only one ADC module, there are two types, PCF8591 and ADS7830. For the projects described in this tutorial, they function the same. Please build corresponding circuits according to the ADC module found in your Kit.

ADC module: PCF8591

ADC module: ADS7830

Model diagram

ADC-module-1

Actual Picture

PCF8591

Model diagram

ADC-module-2

Actual Picture

ADS7830

6.1.2. Circuit with ADS7830

Schematic diagram

ADS7830-Schematic

Hardware connection. If you need any support,please feel free to contact us via:

support@freenove.com

This product contains only one ADC module.

ADS7830-fritizing

6.1.3. Circuit with PCF8591

Schematic diagram

PCF8591-Schematic

Hardware connection.

PCF8591-fritizing

Please keep the chip mark consistent to make the chips under right direction and position.

6.1.4. Sketch

6.1.4.1. Configure I2C (required)

6.1.4.1.1. Enable I2C

There are some I2C chips in this kit like ADC module. The I2C interface of Raspberry Pi is closed by default. You need to open it manually as follows:

Type command in the Terminal:

$ sudo raspi-config

Then open the following dialog box:

../../../_images/Enable-I2C.png

Choose “5 Interfacing Options” -> “P5 I2C” -> “Yes” -> “Finish” in order and restart your RPi later. Then the I2C module is started.

Type a command to check whether the I2C module is started:

$ lsmod | grep i2c

If the I2C module has been started, the following content will be shown. “bcm2708” refers to the CPU model. Different models of Raspberry Pi display different contents depending on the CPU installed:

../../../_images/Enable-I2C-2.png
6.1.4.1.2. Install I2C-Tools

Type the command to install I2C-Tools.

$ sudo apt-get install i2c-tools

Detect the address of I2C device with the following command:

$ i2cdetect -y 1

When you are using the PCF8591 Module, the result should look like this:

../../../_images/Enable-I2C-3.png

Here, 48 (HEX) is the I2C address of ADC Module (PCF8591).

When you are using ADS, the result is as below:

../../../_images/Enable-I2C-4.png

Here, 4b (HEX) is the I2C address of ADC Module (ADS7830).

6.1.4.2. Sketch ADC

First, observe the result after running the sketch, and then learn about the code in detail.

  1. Use Processing to open the file Sketch_06_1_1_ADC.

$ processing ~/Freenove_Kit/Processing/Sketches/Sketch_06_1_1_ADC/Sketch_06_1_1_ADC.pde
  1. Click on “RUN” to run the code.

After the program is executed, Display Window shows the voltage value of the potentiometer and the ADC value. Rotate the potentiometer to change the voltage output by potentiometer.

../../../_images/pr_adc.png

This project contains a lot of code files, and the core code is contained in the file Sketch_06_1_1_ADC. The other files only contain some custom classes.

../../../_images/pr_adc_project.png

The following is program code:

 1/*****************************************************
 2 * Filename    : Sketch_06_1_1_ADC
 3 * Description : Making a voltmeter with Freenove ADC module.
 4 * auther      : www.freenove.com
 5 * modification: 2024/09/04
 6 *****************************************************/
 7import freenove.processing.io.*;
 8
 9//Create a object of class ADCDevice
10ADCDevice adc = new ADCDevice();
11void setup() {
12  size(640, 360);
13  if (adc.detectI2C(0x48)) {
14    adc = new PCF8591(0x48);
15  } else if (adc.detectI2C(0x4b)) {
16    adc = new ADS7830(0x4b);
17  } else {
18    println("Not found ADC Module!");
19    System.exit(-1);
20  }
21}
22void draw() {
23  int adcValue = adc.analogRead(0);    //Read the ADC value of channel 0
24  float volt = adcValue*3.3/255.0;    //calculate the voltage
25  background(255);
26  titleAndSiteInfo();
27
28  fill(0);
29  textAlign(CENTER);    //set the text centered
30  textSize(30);
31  text("ADC: "+nf(adcValue, 3, 0), width / 2, height/2+50);
32  textSize(40);        //set text size
33  text("Voltage: "+nf(volt, 0, 2)+"V", width / 2, height/2);    //
34}
35void titleAndSiteInfo() {
36  fill(0);
37  textAlign(CENTER);    //set the text centered
38  textSize(40);        //set text size
39  text("ADC", width / 2, 40);    //title
40  textSize(16);
41  text("www.freenove.com", width / 2, height - 20);    //site
42}

The code of this project mainly uses PCF8591 class member function analogRead() to read ADC.

1int adcValue = adc.analogRead(0);    //Read the ADC value of channel 0
2float volt = adcValue*3.3/255.0;    //calculate the voltage

About class ADCDevice, PCF8591, ADS7830:

class ADCDevice

This is a base class, and all ADC module classes are subclasses of it. It provides two basic member functions.

public int analogRead (int chn)

This is a unified function name. Different chips have different implement methods. Therefore, specific method is implemented in subclasses.

public boolean detectI2C (int addr)

Used to detect I2C device with a given address. If it exists, it returns true, otherwise it returns false.

class PCF8591 extends ADCDevice

This is a custom class that is used to operate the ADC and DAC of PCF8591.

public PCF8591 (int addr)

Constructor, used to create a PCF8591 class object, parameters for the I2C PCF8591 device address.

public int analogRead (int chn)

Used to read ADC value of one channel of PCF8591, the parameter CHN indicates the channel number: 0,1,2,3.

public byte[] analogRead ()

To read ADC values of all channels of PCF8591.

public void analogWrite (int data)

Write a DAC value to PCF8591.

class ADS7830 extends ADCDevice

This is a custom class that is used to operate the ADC of ADS7830.

public ADS7830(int addr)

Constructor, used to create a ADS7830 class object, parameters for the I2C ADS7830 device address.

public int analogRead(int chn)

Used to read ADC value of one channel of ADS7830, the parameter CHN indicates the channel number: 0,1,2,3,4,5,6,7.