0% found this document useful (0 votes)
305 views

Waveform Generator Using Raspberry Pi Python Code

This document describes a basic waveform generator using a Raspberry Pi that can generate sine, square, and triangular waves. It controls the amplitude and frequency of the waves using buttons and sliders. The generator outputs the waves to a digital to analog converter (DAC) connected to the Raspberry Pi over I2C. Code is provided to generate each type of waveform based on button presses and update the amplitude and frequency values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
305 views

Waveform Generator Using Raspberry Pi Python Code

This document describes a basic waveform generator using a Raspberry Pi that can generate sine, square, and triangular waves. It controls the amplitude and frequency of the waves using buttons and sliders. The generator outputs the waves to a digital to analog converter (DAC) connected to the Raspberry Pi over I2C. Code is provided to generate each type of waveform based on button presses and update the amplitude and frequency values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#Basic Waveform Generator using Raspberry Pi

#Sin,Square and Triangular waves with Amplitude and Frequency Control

from graphics import *


from time import sleep
import math
import board
import busio
import adafruit_mcp4725
i2c = busio.I2C(board.SCL, board.SDA)
# Create a DAC instance.
dac = adafruit_mcp4725.MCP4725(i2c,address=0x60)

CounterValue = 0
Amplitude = 1
Time_Delay = 0.000001

win = GraphWin("Waveform Generator", 400, 400)

#---------------------------Amp-------------
rect = Rectangle(Point(40, 180), Point(120, 220))
rect.setFill("black")
rect.draw(win)

RectMessage = Text(Point(80,200), "Amp")


RectMessage.setSize(12)
RectMessage.setStyle("bold")
RectMessage.setTextColor("white")
RectMessage.draw(win)

#----------------------------Freq----------
rect = Rectangle(Point(40, 270), Point(120, 310))
rect.setFill("black")
rect.draw(win)

RectMessage = Text(Point(80,290), "Freq")


RectMessage.setSize(12)
RectMessage.setStyle("bold")
RectMessage.setTextColor("white")
RectMessage.draw(win)

#----------------------------------------------

#---------------------------Sin Button---------
rect = Rectangle(Point(40, 90), Point(120, 130))
rect.setFill("blue")
rect.draw(win)

RectMessage = Text(Point(80,110), "Sin")


RectMessage.setSize(12)
RectMessage.setStyle("bold")
RectMessage.setTextColor("white")
RectMessage.draw(win)
#------------------------------------------------

#---------------------------Square Button----------
rect = Rectangle(Point(130, 90), Point(210, 130))
rect.setFill("blue")
rect.draw(win)

RectMessage = Text(Point(170,110), "Square")


RectMessage.setSize(12)
RectMessage.setStyle("bold")
RectMessage.setTextColor("white")
RectMessage.draw(win)
#------------------------------------------------

#---------------------------Triangular Button---------
rect = Rectangle(Point(220, 90), Point(300, 130))
rect.setFill("blue")
rect.draw(win)

RectMessage = Text(Point(260,110), "Triangular")


RectMessage.setSize(12)
RectMessage.setStyle("bold")
RectMessage.setTextColor("white")
RectMessage.draw(win)
#------------------------------------------------

Sin = 0
Square = 0
Triangular = 0
xValue = 0
yValue = 0

while True:
MouseCoordinates = win.checkMouse()
if MouseCoordinates is not None:
xValue = MouseCoordinates.getX()
yValue = MouseCoordinates.getY()

#----------------------------Amp Button Detect-------------------------

if(xValue > 40 and xValue < 120 and yValue > 180 and yValue < 220):
Amplitude += 1
if(Amplitude == 10):
Amplitude = 1
#print(Amplitude)
#--------------------------------------------------------------------

#----------------------------Freq Button Detect-------------------------

if(xValue > 40 and xValue < 120 and yValue > 270 and yValue < 310):
Time_Delay *= 10
if(Time_Delay == 1):
Time_Delay = 0.000001
#print(Time_Delay)
#--------------------------------------------------------------------
if(xValue > 40 and xValue < 120 and yValue > 90 and yValue < 130):
Sin = 1
Square = 0
Triangular = 0

if(xValue > 130 and xValue < 210 and yValue > 90 and yValue < 130):
Sin = 0
Square = 1
Triangular = 0

if(xValue > 220 and xValue < 300 and yValue > 90 and yValue < 130):
Sin = 0
Square = 0
Triangular = 1

if Sin == 1 and Square == 0 and Triangular == 0:


#CounterValue += 1
#message.setText(str(CounterValue))
#sleep(1)
for i in range(4095,0,-50):
dac.raw_value = 1400 + int(124 * Amplitude * (math.sin( 2 * math.pi * i
/ 4095)))
sleep(Time_Delay)

if Sin == 0 and Square == 1 and Triangular == 0:


#message.setText(str(CounterValue))
dac.raw_value = 409*Amplitude
sleep(Time_Delay)
dac.raw_value = 0
sleep(Time_Delay)

if Sin == 0 and Square == 0 and Triangular == 1:


#CounterValue = 0
#message.setText(str(CounterValue))
for i in range(0,409*Amplitude,50):
dac.raw_value = i
sleep(Time_Delay)
for i in range(409*Amplitude,0,-50):
dac.raw_value = i
sleep(Time_Delay)

You might also like