GPakeric mm me me ~ aE
A Simple Square-Wave Function Generator with an Project Details
Arduino sre
By Maker Statt
Mary makers rarely use the analog outputs on thelr Arduina, even though ev and vst Roars
they can be useful ina wide variety of applications -such as dimming an Potentiometers
LED. Most paople nave done that atleast once, and cauntiess tutorials Use
an analog /0 pin to drive an LED. This projact shows how to connect License
multiple simple 1/0 components to an Arduino to create an uncomplicated
funetion generator.
om
Get Involved
1x Soldertess breadboard
1X Arduino Due (or any other Arduino with a builtin DAC) ke
2x 20K Patentometer Pavone
‘4x 10K Resistor Becker
1 (only needed if your Arduino doesnt have 2
butters BAC)
‘choosing the Right Arduino
Most Arduino boards can easily convert an analog vortage level to a aighal
Yalue withthe help of a builtin analog-to-cigtal converter (ADC). This t=
the value tat displays wnen performing an analogltead operation on one:
‘of the decicated enaiog /0 pins on an Arduino
However, whet many people dortt knows is that this doess't necesseri
eon that you can algo output an anslog value in the same way. Inetead,
the software wil automatically transform an analagWets cal to a pulse-
‘wieth modulated digital signal instead of an appropriate voltage value
2
Bian:
“The mentioned technique is fine for many applleations, ike dimming an
LED or controling the speed of a motor For this example, however that
simple technique le not good enough because the Arduino needs to
produce voltage values someuinete etwecn LOW and HIGH
‘Therefore, we will use an Arduine witha builtin DAC, ike the Arduino Due
‘or the MK Vidor 4000 board, Note thet Its siso possible fo use an external
Dacitnecessary.
“The Basies of the Circuit
‘As mentioned above, a few simple physical input and output components
‘control the signal thatthe Arduino generates. For thet purpose, this prolect
Uuiized two 20K Ohm potentiometers and two TOK resistors te buld'a
Voltage divider that will reduce the voltage from five volts to 3.3 V. Note
at his may not be necessery onal Ardurnon, a8 most of them should be‘The Basles of the Clreutt
‘As mentioned above, a few simple physical input and output components
Control ine signal that the Arduino generates. For that purpose, this project
Utlized two 20K Ohm potentiometers and two 10K resistors to build a
Voltage divider that will reduce the voltage from five volts to 3.3 V. Note
that this may not be necessary on all Arduinos, as most of nem should be
5V lolerant. The middle pins ofthe potentiometers were then connected to
{wo separate analog input ports on the Arduino. Finally, two pushbutton
switches were added: one for resetting the circuit, and one that allows the
User to change the generated waveform and cycle through @ few settings.
ford +
prrers
(image source: Created by author)
‘The buttons above are used in combination with appropriate pulldown
resistors, along with an LED that indicates that the crcut is on.
‘A mote detailed schematic of the entire project can be seen below:
Lpschematic 4
Leain more by looking at the schematic for the project on Schemest
‘The Importance of the Software
Like many other Arduino-based projects, the software isa vital aspect of
this Dulld In this case, the code reads the user input and detects when the
slate of one of the two potentiometers changes, or when a button gets
pressed. As stated above, the generated wavetorm can be changed with
he of the two push-duttons. The software then generates the Output,
Sepending on the state thatthe circuit I
‘copy code
1 Change these according te your Arcusna!
1 EONPin and ACPI must. be. interrupt. pans
1) please eter to tne documentation. netps! //ivm-arduan.cc/recopy Code
TES Seti
ies ai
14 The maximum value that the analog inputs will read
int 1 = 100; // Low tine of the output waveform
71 the state describes what the output waveform
Unsigned Lon debouncede1ay = 50:
void setup
x
Serial begin(9600);
14 Set yp ald the pins
Pitches. Paks obTPT:
DiniodecruwPIN, INPUT)
pinledecAcc PIN: INPUT):
Binledeceraue, inrury
pinicdeceriMe, tmrur)”
11 Attach interrupts to the pins that the buttons are connect
Steachinterrupt(sugitelPinfoinverrupt(ACe PIN), aceepthange
Steachinterrupe(aigitalpinteinterrupe(runlPin}, chargerunclis
71 Turn the LED on (It"s just a simple poner-on indicator)
Gigieainetee(Le0.PIK, HIGH)
acceptenangeOs
>
5 changes the waveform of the output if the appropriate, button
3 to pass before you can call 4t agsin.
Af Cqmillis() - lastchange) > debounceDelay)
‘
17 Switch to the next state
Aftstate = 2)
lastchange = ailliseo
aceepechaneec‘dteachinterruptcdigitalpintointerrupt(ace FN), aceeptchange,
SteachtnterrupeCdigitalpinteinterrupe(rin_Pin}, chareruneeis
11 Turn the LED on (4t*s Just a simpte power-on indicator)
GigitalwritetLeo PIN, HIGH)
‘eceptchanged:
5 gpanees the mavetorn of, he outpus if, hw appropriate button
1ie"jaen olfre'you'tan GOTH sgn,
vola changerunction¢>
¥
$F ((ei11i50) — destchengs) > doboucetelay)
«
iecotate 2
Aastchange = a1it1s()
atcepeensngec)?
»
>
void acceptchangac)
v
bh = clawpcanalomtead«iriwey, 1, Liurr):
11> Gistptanalogtendciriwe), 3! LIM);
cho.
angie"= 0:
>
ant clompCint value, int min, int max)
«
Afevalue < min)
scvatue > max
return value:
old dosauarewavac
a pee eee ee eer
17 Instead, "st ralies on counter variables that it increases
11 tate, “uel ch (eho counter variable for the high time)
14 the: specitied hagh tise (which can be changed With the pot
1 IF that's the case. the function pulls the output pin LOM,analoghritenesoluti
nes)
int Itenp = elamptanalogseadcLTiMe), 1 LIMIT)
ifdtem 1 LT heen hy
17 output 2 waver
depending on the current state
break
real
,
>
Note that this code trgete Arduino with 2 builtin DAC with a minimum
Fesolution of 10 bis. If youte using a ferent DAC, make Sure to adjust
the values accordingly!
Using an Arduino without a Built-in DAC
‘Connecting al components to 6 compatible Arduino as described above
‘means that everything should be ready to go once everything is plugged in
Users without a builtin DAC for their Arduino, however will have to add an
{extemal DAG of some sort. There are many different methods that You can
Ulize to receive a tue analog output, bul one easy recommendation Is
Using a simple external 2c digato-analog converter module
Leatm more by ooking atthe schematic fr the project on Scheme
ote that vol alsa have to undate the code accordingly Eor thLearn more by looking atthe schematic forthe project on Scheme:
Note that youl also have to update the code accordingly. For this
patticular DAC module, tne most obvious crioice Isto
‘dovnload and use
he official Adal iirary that comes with. Then, instead of doing the
[nalogWrte operations, send the digital value ta the DAC
1y Replace this
aralogersteCQUT_PIN, val)
Gacrsetvoltagecval, false):
Furthermore, add the following line tothe setup method
copy Code
For addtional suppor, take a look at the oficial documentation,
‘Some Final Thoughts for your Project
Wen attempting to build this project, Keep in mind thatthe resuiting
Tunetion generator wll not be very accurate due to how the signal gets
‘generated. In this project, the maximum frequency of the irished product
‘will neaviy depend on the Arduino you use and ite internal clock speed
Even with a his in mind, the prjest can teach alot apout algal mpuns
‘and analog /0 in general, a2 well 92 loaps and states ma program
‘This project demonstrates how you can attach multiple physical inputs to
{an Arduino to alow a user to tweak how te finished product operates. For
{that purpose, the Arduino acts as a very base function generator that can
continuously output one of three wavefotms:
Make sure you use an Arduino with a built-in DAG. you dor't have one,
you.can add an external DAC of some zort, which will tnen generate a tue
‘analog output Furthermore, you should Keep in mind that this is a base
funtion generator. The output cant go above +8'V, and i also cant go
Below zero Volis.Adaitionally the frequency and accuracy are limited and
depend on the Arduino and its clock speedKey Parts and Components
‘ARDUINO DUE ATSAMAXBE
EVAL BRO.
‘x000062
40.30
[Adatritindonies Ue
EVAL BOARO MCPA725,
err bac
935
S495,
BREADBRD TERM STRIP.
320x200"
FrT0096
$290
2
co
OT 20k OHM 1/5W
PLASTIC LINEAR.
PGoKN-00C13820K
5086KNOB KNURLED 0.236"
METAL
OE sA5037
$761
‘SWITCH TACTILE SPSTNO
005812
MurP1230
$0.10
eet re
LED RED CLEAR SMM
ROUND T/H
(5038-RAN-CZOCOAAI
sos
e
_stockpte eens ne
RES 10K OHM 5% 1/4
AXIAL
criastioKa
50.10e
‘Stackpole Electrons ne
RES 10k OHM 5% 1/4W
AXIAL
CFi4JT10K9
$0.10
NM
‘Stackpole Electrons ne
RES 220 OHM 5% 1/4
AXIAL
crMi4sT220R
$010
¢
_Sparkrun electronics
JUMPER WIRE M/M 6°
20PCs.
PRT-12795
$1.95
Adal obey Pars to cant
1050-1049.ND
1528-1010ND
1738-1326ND
987-1302ND
226-3128ND
(679-2428ND
(C5038-RAN-CZOCOAAT-ND
GFIAJTIOKOCTND,
'S2200C7-ND
1588-1512.ND