Example Code for Raspberry Pi-Generate a Sine Wave
This article guides users through generating sine waves using a Raspberry Pi and a 12-Bit I2C DAC Module, covering hardware setup, software installation, wiring diagram, and code execution to observe and adjust waveform characteristics.
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
-
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.
-
REF_VOLTAGE can be changed according to the calibration section. If no calibration is done, enter 5000 (5V) or 3300 (3.3V) depending on the VCC power supply.
-
In the Terminal, type in the following commands and press Enter to run the sample code:
cd DFRobot_MCP4725/RaspberryPi/Python/OutputSin
sudo python OutputSin.py
-
Use an oscilloscope to observe the VOUT output voltage waveform.
-
The user can modify the parameters in the function mcp4725.outputSin() to change the amplitude, frequency and DC offset of the sine wave.
Result
- A full sine wave of 2.5V in amplitude (5V peak to peak), 10Hz and 2.5V DC bias can be observed.
Additional Information
def output_sin(self,amp,freq,offset):
'''!
@fn outputSin
@brief Output a sine wave.
@param amp amp value, output sine wave amplitude range 0-5000mv
@param freq freq value, output sine wave frequency
@param offset offset value, output sine wave DC offset
'''
Was this article helpful?
