In this project, we will use the ADC function of an ADC Module to read the voltage value of a potentiometer.
What is a potentiometer?
A potentiometer is a type of position sensor that can adjust voltage. The potentiometer was invented in 1872 to control the amount of electricity going to a component by Thomas Edison at the age of 25.
Some uses for potentiometers today are:
•Volume controls on audio equipment
•Medical equipment
•Injection mold machines
•Position transducers
The following are the materials you will need for this project:
The I2C interface in Raspberry Pi is disabled by default. You will need to open it manually and enable the I2C interface as follows:
Type command in the Terminal:
sudo raspi-config
Then open the following dialog box to the right:
Choose “5 Interfacing Options” then “P5 I2C” then “Yes” and then “Finish” in this order and restart your RPi.
The I2C module will then be 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.
Next, type the command to install I2C-Tools. It is available with the Raspberry Pi OS by default.
sudo apt-get install i2c-tools
I2C device address detection:
i2cdetect -y 1
When you are using the PCF8591 Module, the result should look like this:
Here, 48 (HEX) is the I2C address of ADC Module (PCF8591)
When you are using ADS, the result should look like this:
Here, 4b (HEX) is the I2C address of ADC Module (ADS7830)
sudo apt-get install python-smbus
sudo apt-get install python3-smbus
For C code for the ADC Device, a custom library needs to be installed.
1. Use cd command to enter folder of the ADC Device library.
cd ~/Freenove_Kit/Libs/C-Libs/ADCDevice
2. Execute command below to install the library.
sh ./build.sh
A successful installation, without error prompts, is shown below
Next, we will execute the code for this project.
First, observe the project result, and then learn about the code in detail.
1. Use cd command to enter 07.1.1_ADC directory of C code.
cd ~/Freenove_Kit/Code/C_Code/07.1.1_ADC
2. Use following command to compile “ADC.cpp” and generate the executable file “ADC”.
g++ ADC.cpp -o ADC -lwiringPi -lADCDevice
3. Then run the generated file “ADC”.
sudo ./ADC
After the program is executed, adjusting the potentiometer will produce a readout display of the potentiometer voltage values in the Terminal and the converted digital content.
For the source code of the project download the PDF at https://freenove.com/fnk0020/