Example Code for Raspberry Pi-Calibration and Adjustable Analog Voltage Output
A detailed guide on calibrating and adjusting the analog voltage output of a Raspberry Pi using a 12-Bit I2C DAC module. It covers necessary hardware and software preparations, wiring diagrams, and step-by-step Python code execution, ensuring accurate voltage output. The guide also includes methods to modify Python programs using IDLE or nano editor, and emphasizes the importance of precise calibration with a multimeter to achieve desired voltage levels.
Hardware Preparation
- Raspberry Pi 4 Model B (or similar) * 1
- Gravity: 12-Bit I2C DAC Module * 1
- Gravity 4-pin sensor wire (comes with Gravity 12Bit DAC Module) * 1
- Digital multimeter (optional) x 1
- Oscilloscope (optional) x 1
Software Preparation
- Download and install the Gravity 12-Bit I2C DAC RaspberryPi library
- RASPBIAN
Wiring Diagram
Other Preparation Work
- Start the I2C interface of the Raspberry Pi. If it is already open, skip this step. Open Terminal, type the following command, and press Enter:
sudo raspi-config
-
Then use the up and down keys to select “5 Interfacing Options” -> “P5 I2C” and press Enter to confirm “YES”. Reboot the Raspberry Pi.
-
Installing Python libraries and git (networking required). If it is already installed, skip this step. In the Terminal, type the following commands, and press Enter:
sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus git
git clone https://github.com/DFRobot/DFRobot_MCP4725.git
- After download the driver library.
- Users are required an additional high-precision digital multimeter to complete the calibration, the specific steps are as follows:
- Connect the module to the Raspberry Pi according to the connection diagram above and set I2C address to 0x60 by ADDR switch on the module. If I2C address 0x61 is preferred, you need to modify the parameter of the function mcp4725.setAddr_MCP4725() to MCP4725A0_IIC_Address1.
- Open Python codes OutputVoltage.py and change both REF_VOLTAGE and OUTPUT_VOLTAGE to 5000 (if the module uses 3.3V, change to 3300).
- In the Terminal, type in the following commands and press Enter to run the sample code:
cd DFRobot_MCP4725/python/raspberrypi/examples
sudo python output_voltage.py
-
Use the multimeter to measure the output voltage of VOUT and change the value of REF_VOLTAGE accordingly. For example, VOUT = 4950mV, the "#define REF_VOLTAGE 5000" should be revised to "REF_VOLTAGE 4950". Calibration completed.
-
Change the value of OUTPUT_VOLTAGE (unit in mV) to your desired analog output voltage. This value should be smaller than REF_VOLTAGE or the maximum output voltage will be limited to REF_VOLTAGE. Before using the sample code below, write the calibration value to REF_VOLTAGE first to get a more accurate output voltage.
- There are two ways to open and modify Python programs:
1.Use the IDLE integrated development environment to modify Python programs in a graphical environment. Click "File Manager" in the menu bar, and then type the following path
/home/pi/DFRobot_MCP4725/RaspberryPi/Python/OutputVoltage
Right click on the Python source code "OutputVoltage.py" in the directory. Select “Python 3 (IDLE)” from the menu and edit the Python program in the pop-up window. After editing, use the shortcut “Ctrl S” to save and closed window.
2.Use the nano editor to modify Python programs in the Terminal. In the Terminal, enter the following commands:
cd /DFRobot_MCP4725/RaspbeeryPi/python/OutputVoltage
nano OutputVoltage.py
After entering the commands, a Python program is opened using the nano editor. After completing the code modification, use the shortcut “Ctrl O” to save, press Enter to confirm, and then use the shortcut “Ctrl X” to close the nano editor.
Was this article helpful?
