Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
CesarSound
Published © GPL3+

Generating waveforms with Arduino Uno

Use mathematical functions to generate waveforms such as square, saw and sine wave and apply a low pass filter.

BeginnerShowcase (no instructions)8,352
Generating waveforms with Arduino Uno

Things used in this project

Story

Read more

Schematics

Schematics

Needed to work.

Code

ARDUINO UNO - WAVEFORM GENERATOR AND FILTER

C/C++
Load this sketch to Arduino Uno
/**************************************************************************************
  ARDUINO UNO - WAVEFORM GENERATOR AND FILTER - JULIO CESAR - JCR - 09/21
 **************************************************************************************/
/* Instructions: connect Arduino Uno to PC or Notebook via USB cable, choose one of the functions to be plotted
  (SAW, SQR, FILTER and SINE), load the sketch in Arduino Uno, go to the Tools menu and open the Serial Plotter. */

byte filter;
byte saw;
byte sqr;
int sine;
int angle;

void setup() {
  Serial.begin(19200);
}

void loop() {
  delay(5);
  saw_square_gen();
  filter_lowpass();
  sine_gen();

  //Comment out one the below Serial.println() waveform you want to plot (one at a time):
  //Serial.println(saw);
  //Serial.println(sqr);
  //Serial.println(filter);
  Serial.println(sine);
}

void saw_square_gen() {     //SAW & SQUARE WAVE GEN
  saw = saw + 1;
  if (saw > 127 && saw < 256) sqr = 255; else sqr = 1;
}

void sine_gen() {            //SINE WAVE GEN
  angle = angle + 1;  if (angle > 360) angle = 0;
  //sine = 50 * sin((angle * PI / 180));
  sine = 50 * sin(angle * 0.0174532925);
  //sine = abs(sine);
}

void filter_lowpass() {      //DIGITAL FILTER
  filter = filter * 0.9 + sqr * 0.1;
}

Credits

CesarSound
6 projects • 190 followers

Comments