Professional Documents
Culture Documents
COM/SIMPLE-RASPBERRY-PI-PROJECTS-FOR-BEGINNERS)
Microcontrollers work only with digital values but in real world we have to deal with Analog signals. That’s why ADC (Analog to Digital Converters)
(https://circuitdigest.com/tutorial/what-is-adc-analog-to-digital-converters) is there to convert real world Analog values into Digital form so that
microcontrollers can process the signals. But what if we need Analog signals from digital values, so here comes the DAC (Digital to Analog Converter).
DAC can be used in many applications such as Motor control, Control Brightness of the LED Lights, Audio Ampli er (https://circuitdigest.com/audio-
ampli er-circuits), Video Encoders, Data Acquisition Systems etc.
Components Required
Raspberry Pi 3 B+ (With Raspbian OS installed)
MCP4725 DAC IC
16x2 LCD display
Breadboard
Connecting Wires
This IC has 12-Bit resolution. This means we use (0 to 4096) as input to provide the voltage output with respect to reference voltage. Maximum reference
voltage is 5V.
For Example if we use 5V as reference voltage and let’s assume that digital value is 2048. So to calculate the DAC output.
Pinout of MCP4725
GND for input
GND
This IC can be controlled using the I2C communication which requires only two wires SCL and SDA. By default, the I2C address for MCP4725 is 0x60. Now
we know about I2C communication in Raspberry pi.
Below is the Pin Diagram of Raspberry Pi 3 Model B+, and I2C pins GPIO2 (SDA) and GPIO3 (SCL) are used in this tutorial.
By default, I2C is disabled in Raspberry Pi. So rst it must be enabled. To enable the I2C in Raspberry Pi
Now in order to start communication with the MCP4725 IC, the Raspberry Pi must know its I2C address. To nd the address rst connect the SDA and SCL
pin of MCP4725 to the SDA and SCL pin of Raspberry Pi. Also connect the +5V and GND pins.
Now open the terminal and type below command to know the address of connected I2C device,
After nding the I2C address now its time to install the necessary libraries for using MCP4725 with Raspberry Pi.
In order to use MCP4725 DAC board with I2C bus of Raspberry Pi, a Adafruit MCP4725 library is used. To download and install the library follow these
steps:
2. Next open a terminal and run the following lines one by one.
3. After successful installation now the Adafruit MCP4725 library can be imported in any python script by using the line
Import Adafruit_MCP4725
A LCD is used in this project to display the DAC and analog voltage values so to download and install the LCD library in Raspberry Pi follow these steps:
1. Open a terminal window and run the following lines one by one.
2. After installation of LCD library now the Adafruit_python_CharLCD can be used from any python script by using the following line
Now the Raspberry Pi is ready to code for Digital to Analog converter so let’s connect the circuit as shown in the gure below.
(/fullimage?
i=circuitdiagram_mic/Circuit-Diagram-for-Interfacing-MCP4725-12-Bit-DAC-with-Raspberry-Pi.png)
LCD Raspberry Pi 3 B+
VSS GND
VDD +5V
RS GPIO25
RW GND
E GPIO24
D4 GPIO23
D5 GPIO17
D6 GPIO18
D7 GPIO22
A +5V
K GND
VCC +5V -
In this program a digital value of 0-4096 is sent from Raspberry Pi to the MCP4725 via I2C bus to produce an analog output voltage of 0 to 5V which can
be veri ed with the multimeter. Both the digital and analog values are displayed on the 16x2 LCD (https://circuitdigest.com/article/16x2-lcd-display-
module-pinout-datasheet). In our program the digital value is sent with an increment of 150 using for loop (0,150,300,450…4050). Let’s see the program in
detail.
First include all the required libraries. Here LCD, MCP4725 and time library are used.
Next de ne the LCD pins along with the no. of row and columns. We know that 16X2 LCD has 2 rows and 16 columns. Learn more about interfacing LCD
with Raspberry Pi (https://circuitdigest.com/microcontroller-projects/raspberry-pi-lcd-display-tutorial) here.
lcd_rs = 25
lcd_en = 24
lcd_d4 = 23
lcd_d5 = 17
lcd_d6 = 18
lcd_d7 = 22
lcd_backlight = 4
lcd.message('Circuit Digest')
time.sleep(2.0)
lcd.message('\nDAC using Rpi')
time.sleep(5.0)
lcd.clear()
In the next line, a DAC instance is created with the I2C address of the MCP4725 DAC IC. My board has an address of 0x60, change it according to your
board.
dac = Adafruit_MCP4725.MCP4725(address=0x60)
Next a for loop is used inside while loop to change the digital value x that is sent to MCP4725 via I2C bus. The for loop range is (0,4095,150). The x values
vary from 0 to 4050 with an increment of 150.
while True:
for x in range(0,4097,150):
dac.set_voltage(x)
Depending upon the digital value the analog value is calculated using the formula where 5 is reference voltage and x is digital value.
voltage = x/4096.0*5.0
Then the Digital value & Analog value are displayed on the LCD with a delay of 2 seconds using the following lines
lcd.cursor_pos = (0,0)
lcd.message("DAC Value: " + str(x))
lcd.message("\nAnalogVolt: %.2f" % voltage)
time.sleep(2)
Here the Digital value is shown in the rst row and analog value in second row of LCD display. A multimeter is also connected to the MCP4725 Output Pins
to verify the analog voltage.
Code
import Adafruit_CharLCD as LCD
import Adafruit_MCP4725
import time
lcd_rs = 25
lcd_en = 24
lcd_d4 = 23
lcd_d5 = 17
lcd_d6 = 18
lcd_d7 = 22
lcd_backlight = 4
lcd.message('Circuit Digest')
time.sleep(2.0)
lcd.message('\nDAC using Rpi')
time.sleep(5.0)
lcd.clear()
dac = Adafruit_MCP4725.MCP4725(address=0x60)
while True:
for x in range(0,4097,150):
print(x)
dac.set_voltage(x)
lcd.cursor_pos = (0,0)
lcd.message("DAC Value: " + str(x))
voltage = x/4096.0*5.0
lcd.message("\nAnalogVolt: %.2f" % voltage)
time.sleep(2)
lcd.clear()
Video
Raspberry Pi DAC Tutorial: Interfacing MCP4725 12-Bit Digital-to-Analog Converter with Raspberry Pi
Email Address *
Name
Country
United States of America
Subscribe
RELATED CONTENT
(/article/top-media-server-software-for-music-streaming-on-raspberry-pi)
(/microcontroller-projects/optical-character-recognition-ocr-using-tesseract-on-raspberry-pi)
(/microcontroller-projects/raspberry-pi-based-jarvis-themed-speaking-alarm-clock)
(/microcontroller-projects/how-to-run-spotify-on-raspberry-pi-using-mopidy-music-server)
(/microcontroller-projects/diy-raspberry-pi-nas-server-using-samba)
(/microcontroller-projects/raspberry-pi-retropie-gaming-console)
(/microcontroller-projects/wireless-rf-communication-between-arduino-and-raspberry-pi-using-nrf24l01)
(/microcontroller-projects/how-to-install-diet-pi-on-raspberry-pi)
NEXT POST
Interfacing DC Motor with AVR Microcontroller Atmega16 (https://circuitdigest.com/microcontroller-projects/interfacing-dc-motor-
with-atmega16-avr-microcontroller)
COMMENTS
(http://bit.ly/mouser-
memory-cards )
Vishay microBRICK™ DC/DC Regulator Modules (http://bit.ly/mouser-vishay )
Synchronous buck regulators with integrated power MOSFETs and an inductor.
(http://bit.ly/mouser-
vishay )
Littelfuse 8.0SMDJ TVS Diodes (http://bit.ly/mouser-littlefuse )
Protect sensitive electronic equipment from voltage transients induced by lightning and other transient voltage events.
(http://bit.ly/mouser-
littlefuse )
TE Connectivity's Discrete Magnetics for Ethernet (http://bit.ly/mouser-te-connectivity )
Feature an extended -40°C to 105°C temperature range with 260°C re ow capability.Feature an extended -40°C to 105°C temperature range with 260°C...
(http://bit.ly/mouser-
te-connectivity )
Texas Instruments DRV5013 Hall-Effect Sensors (http://bit.ly/mouser-hall-sensor )
Chopper-stabilized Hall-Effect Sensors that offer a magnetic sensing solution with superior sensitivity stability over temperature and integrated...
(http://bit.ly/mouser-
hall-sensor )
Texas Instruments bq296 Li-Ion Battery Protectors (http://bit.ly/mouser-texas )
High-accuracy, low-power overvoltage protector with a 1mA regulated output supply.High-accuracy, low-power overvoltage protector with a 1mA...
(http://bit.ly/mouser-
texas )
Bourns P4SMA-Q Transient Voltage Suppressor Diode Series (http://bit.ly/mouser-bourns )
Offers breakdown voltages from 15V up to 68V typical fast response times >1.0 picosecond.Offers breakdown voltages from 15V up to 68V typical...
(http://bit.ly/mouser-
bourns )
Digilent FMC Pcam Adapter Quad Version (http://bit.ly/mouser-digilent )
Add cameras to FPGA or APSoC development boards with a high-speed, FMC connector.
(http://bit.ly/mouser-
digilent )
NEWS ARTICLES PROJECTS
MLX90821- MEMS Pressure Sensor for Evaporation Systems in ICE and Hybrid Vehicles (/news/mlx90821-mems-pressure-sensor-for-
evaporation-systems-in-ice-and-hybrid-vehicles)
(/news/mlx90821-
mems-
pressure-
sensor-for-
evaporation-
systems-in-
ice-and-
hybrid-
vehicles)
Single-Chip ASIL-D-Compliant Battery Monitor IC for Mid-to-Large Cell Count Con gurations (/news/single-chip-asil-d-compliant-battery-
monitor-ic-for-mid-to-large-cell-count-con gurations)
(/news/single-
chip-asil-d-
compliant-
battery-
monitor-ic-for-
mid-to-large-
cell-count-
con gurations)
Key Highlights of The Things Conference India - Asia’s largest conference on LoRaWAN & IoT (/news/key-highlights-the-things-
conference-india-asias-largest-conference-on-lorawan-iot)
(/news/key-
highlights-the-
things-
conference-
india-asias-
largest-
conference-
on-lorawan-
iot)
(/news/5g-
automotive-
and-
transportation-
connectivity-
solutions)
AOZ8621UNI - High Surge TVS Diodes for USB Type-C Protection (/news/aoz8621uni-high-surge-tvs-diodes-for-usb-type-c-protection)
(/news/aoz8621uni-
high-surge-
tvs-diodes-
for-usb-type-
c-protection)
TI WHITEPAPERS
& SOLUTIONS
Connect with us on social media and stay updated with latest news, articles and projects!
(https://www.linkedin.com/company/circuit-
(https://www.facebook.com/circuitdigest/)
(https://twitter.com/CircuitDigest)
(https://www.youtube.com/channel/UCy3CUAIYgZdAOG9k3IPdLmw
(https://www.instagram.com/circuit_digest/)
(https://www.pinterest.com/circuitdigest/)
digest/)
CATEGORIES
POPULAR
NEWSLETTER
Enter your email
Subscribe
Privacy Policy (http://circuitdigest.com/privacy-policy) | Cookie Policy (https://circuitdigest.com/cookie-policy) | Terms of Use (https://circuitdigest.com/terms-of-use) | Contact
Us (http://circuitdigest.com/contact) | Advertise (http://circuitdigest.com/advertise)