Raspberry Pi & Analog to Digital Conversion (ADC)
by drselim in Circuits > Raspberry Pi
6298 Views, 2 Favorites, 0 Comments
Raspberry Pi & Analog to Digital Conversion (ADC)
In this instructablewe'll see how to use Microchip's MCP3208 Analog to Digital Converter with the Rasperry Pi using gpiozerolibrary in Python. We'll convert the outputs of the potentiometers to digital with MCP3208 and then use these values to drive the LEDs with the PWMLED object of the gpiozero library.
Supplies
Raspberry Pi (4 is used in this instructable), MCP3208 ADC, jumper wires, 3 LEDs(different colours preferred), 3*330 Ohm Resistors, 3 Potentiometers (10K used in this instructable), gpiozero library installed.
Connections
The outputs of the three potentiometers will be connected to the channels 1,2 and 3 of the MCP3208
The Vdd and the Vref will be connected to the 3.3 Volts output of the Raspberry Pi
The Analog Ground and the digital ground pins of the MCP3208 will be connected to the groud pin of the Raspberry Pi.
The clock pin will be connected to the GPIO11-the SCLK pin on the Raspbery Pi.
The chip select/shutdown pin will be connected to the GPIO 8 pin of the Raspberry Pi.
The Dout pin will be connected to GPIO9 Master In Slave Out(MISO) pin of the Raspberry Pi.
The Din pin will be connected to GPIO10 Master Out Slave In(MOSI) pin of the Raspberry Pi.
The three LEDs will be connected to GPIO2 ,GPIO3 and GPIO4 pins.
Enabling SPI and Python Code
In order to work with the MCP3208, first you should enable SPI in Raspberry Pi. To do that in the terminal window enter the following command:
sudo raspi-config
In the Raspberry Pi Software Configuration Tool (raspi-config), choose:
>Interfacing Options
>SPI
>Would you like the SPI interface to be enabled? YES
After you enable SPI, you can use the following code to control the LEDs' brightness by using the potentiometers:
-------------CODE-------------------
from gpiozero import PWMLED, MCP3208
Red_Led = PWMLED(2)
Yellow_Led = PWMLED(3)
Blue_Led = PWMLED(4)
red_pot = MCP3208(channel=1)
yellow_pot = MCP3208(channel=2)
blue_pot = MCP3208(channel=3)
while True:
Red_Led.value = red_pot.value
Yellow_Led.value = yellow_pot.value
Blue_Led.value = blue_pot.value
-------------CODE-------------------(don't forget the indents for the lines in the while True Loop)
I hope you enjoy this instructable, to see similar instructables & videos like this, please follow me here and subscribe to my YT channel: