Example Code for Raspberry Pi-Generate a Triangle Wave
Generate a triangle wave with adjustable amplitude, frequency, DC offset, and duty cycle on Raspberry Pi.
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/OutputTriangle
sudo python OutputTriangle.py
-
Use an oscilloscope to observe the VOUT output voltage waveform.
-
The user can modify the parameters in the function mcp4725.outputTriangle() to change the amplitude, frequency, DC offset and duty cycle of the Triangle wave.
Result
- A full triangular wave of 5V (peak-to-peak), 10Hz, duty cycle of 50% without DC bias can be observed.
Additional Information
outputTriangle(amp,freq,offset,dutyCycle)
'''!
@fn outputTriangle
@brief Output a sine wave.
@param amp amp value, output triangular wave amplitude range 0-5000mv
@param freq freq value, output the triangle wave frequency
@param offset offset value, output the DC offset of the triangle wave
@param dutyCycle dutyCycle value, set the rising percentage of the triangle wave as a percentage of the entire cycle.
@n Value range 0-100 (0 for only the decline of 100, only the rise of paragraph)
'''
def output_triangle(self,amp,freq,offset,dutyCycle):
- Change the dutyCycle to 0 while the other parameters remain unchanged. That is "DAC.outputTriangle (5000,10,0,0)", which can generate a sawtooth wave of 5V in amplitude and 10Hz.
Was this article helpful?
