OLED With Piezo Playing Christmas Songs With Arduino

by sister mariaM in Circuits > Arduino

381 Views, 3 Favorites, 0 Comments

OLED With Piezo Playing Christmas Songs With Arduino

oled and piezo1.jpg
oled and piezo1.jpg
oled and piezo1.jpg

This Instructables will explain how an OLED and Piezo can play Christmas songs with Arduino

Supplies

oled and piezo1.jpg

Tools used;

Arduino or equivalent

USB cable

computer with Arduino IDE

OLED

piezo

wires or jumpers

Code


OLED connections ;

connect SDA to SDA on Arduino or A4

connect SCL to SCL on Arduino or A5

Connect ground to ground close to SDA and SCL

connect VCC to 5 volts on Arduino


Piezo ;

connect ground to same ground as OLED(see breadboard)

connect piezo to Arduino 11(positive port on Piezo )



Theory

oled and piezo1.jpg
OLED and piezo jpg.jpg

This projects consists of Arduino and OLED and piezo .

An OLED means Organic Light Emitting Diode It is a display where millions of pixels produce their light .They are used in TV ,and cell phones and displays.

The OLED 0.96 inch is a compact efficient display It uses 128x64 pixels .It doesn't require a backlight .This enhances the image and reduces power consumption

The other component is a The piezo speaker consists of a piezo crystal between 2

crystals .If you apply voltage across the crystals they will push one conductor and pull on the other .This action of pushing and pulling produces sound .(see image of piezo)(see video We wish you a Merry Christmas)

The Code is read by Arduino and produces notes for the song. The song is heard from the piezo .

Here are some short videos with songs(not in entire ,I had limited space for videos);The projects plays all the songs.


https://vimeo.com/manage/videos/1225413465


https://vimeo.com/manage/videos/1225412795


Here is the code for Arduino;


#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>


#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

#define OLED_RESET -1

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#define speaker 11


// Tempos by common musical names

#define grave 40

#define largo 46

#define lento 52

#define adagio 56

#define larghetto 60

#define adagietto 66

#define andante 72

#define andantino 80

#define maestroso 88

#define moderato 100

#define allegretto 104

#define animato 120

#define allegro 132

#define allegro_assai 144

#define vivace 160

#define presto 184

#define prestissimo 208


// Note frequencies (Hz)

#define note_C4 262

#define note_CS4 277

#define note_D4 294

#define note_DS4 311

#define note_E4 330

#define note_F4 349

#define note_FS4 370

#define note_G4 392

#define note_GS4 415

#define note_A4 440

#define note_AS4 466

#define note_BF4 466

#define note_B4 494

#define note_C5 523

#define note_CS5 554

#define note_D5 587

#define note_DS5 622

#define note_E5 659

#define note_F5 698

#define note_FS5 740

#define note_G5 784


float default_tempo = float(animato);

float timings[7];


#define semib timings[0] // 4 beats

#define dot_minim timings[1] // 3 beats

#define minim timings[2] // 2 beats

#define crot timings[3] // 1 beat

#define quav timings[4] // 1/2 beat


String message = "Merry Christmas "; // Text to scroll

int textX; // X-coordinate for scrolling

unsigned long lastDisplayUpdate = 0;


// Forward declarations

void set_tempo(float bpm);

void play(int note_freq, float duration_ms);

void rest(float duration_ms);

void updateDisplay();


void we_wish_you_a_merry_christmas();

void o_come_all_ye_faithful();

void away_in_a_manger();

void ding_dong_merrily();

void good_king_wenceslas();

void the_first_nowell();

void the_holly_and_the_ivy();

void we_three_kings();

void silent_night();


void setup() {

Serial.begin(9600);

pinMode(speaker, OUTPUT);


if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {

Serial.println(F("SSD1306 allocation failed"));

for(;;);

}


display.clearDisplay();

display.setTextSize(2);

display.setTextColor(SSD1306_WHITE);

textX = SCREEN_WIDTH;


set_tempo(default_tempo);

}


void loop() {

we_wish_you_a_merry_christmas();

o_come_all_ye_faithful();

away_in_a_manger();

ding_dong_merrily();

good_king_wenceslas();

the_first_nowell();

the_holly_and_the_ivy();

we_three_kings();

silent_night();

}


// Sets song beat speeds

void set_tempo(float bpm) {

float crotchet_ms = (60.0 / bpm) * 1000.0;

timings[0] = crotchet_ms * 4.0;

timings[1] = crotchet_ms * 3.0;

timings[2] = crotchet_ms * 2.0;

timings[3] = crotchet_ms;

timings[4] = crotchet_ms / 2.0;

}


// Smooth screen updater (Runs non-stop)

void updateDisplay() {

unsigned long currentMillis = millis();

if (currentMillis - lastDisplayUpdate >= 30) {

lastDisplayUpdate = currentMillis;


display.clearDisplay();

display.setCursor(textX, 20);

display.print(message);

display.display();


textX -= 1;

int textWidth = message.length() * 12;

if (textX < -textWidth) {

textX = SCREEN_WIDTH;

}

}

}


// Non-blocking Note Player

void play(int note_freq, float duration_ms) {

unsigned long start = millis();


// Tone sounds for 90% of the duration

tone(speaker, note_freq);

while (millis() - start < (unsigned long)(duration_ms * 0.9)) {

updateDisplay(); // Keeps OLED scrolling while note plays!

}


noTone(speaker);

while (millis() - start < (unsigned long)duration_ms) {

updateDisplay(); // Keeps OLED scrolling during note gap

}

}


void rest(float duration_ms) {

unsigned long start = millis();

while (millis() - start < (unsigned long)duration_ms) {

updateDisplay();

}

}


// --- FULL SONGS ---


void we_wish_you_a_merry_christmas() {

set_tempo(default_tempo);

for (uint8_t repeat = 1; repeat <= 2; repeat++) {

play(note_D4, crot); play(note_G4, crot); play(note_G4, quav); play(note_A4, quav);

play(note_G4, quav); play(note_FS4, quav); play(note_E4, crot); play(note_E4, crot);

play(note_E4, crot); play(note_A4, crot); play(note_A4, quav); play(note_B4, quav);

play(note_A4, quav); play(note_G4, quav); play(note_FS4, crot); play(note_D4, crot);

play(note_D4, crot); play(note_B4, crot); play(note_B4, quav); play(note_C5, quav);

play(note_B4, quav); play(note_A4, quav); play(note_G4, crot); play(note_E4, crot);

play(note_D4, quav); play(note_D4, quav); play(note_E4, crot); play(note_A4, crot);

play(note_FS4, crot); play(note_G4, minim); rest(crot);

}

}


void o_come_all_ye_faithful() {

set_tempo(default_tempo);

for (uint8_t repeat = 1; repeat <= 2; repeat++) {

play(note_G4, crot); play(note_G4, minim); play(note_D4, crot); play(note_G4, crot);

play(note_A4, minim); play(note_D4, minim); play(note_B4, crot); play(note_A4, crot);

play(note_B4, crot); play(note_C5, crot); play(note_B4, minim); play(note_A4, crot);

play(note_G4, crot); play(note_G4, minim); play(note_FS4, crot); play(note_E4, crot);

play(note_FS4, crot); play(note_G4, crot); play(note_A4, crot); play(note_B4, crot);

play(note_FS4, minim); play(note_E4, crot + quav); play(note_D4, quav); play(note_D4, dot_minim);

rest(crot);

}

}


void away_in_a_manger() {

set_tempo(moderato);

for (uint8_t repeat = 1; repeat <= 2; repeat++) {

play(note_C4, crot); play(note_F4, crot); play(note_F4, crot); play(note_G4, quav);

play(note_A4, quav); play(note_F4, crot); play(note_F4, crot); play(note_A4, quav);

play(note_BF4, quav); play(note_C5, crot); play(note_C5, crot); play(note_D5, crot);

play(note_BF4, minim); play(note_G4, quav); play(note_A4, quav); play(note_BF4, crot);

play(note_BF4, crot); play(note_C5, crot); play(note_A4, crot); play(note_A4, crot);

play(note_F4, quav); play(note_A4, quav); play(note_G4, crot); play(note_D4, crot);

play(note_E4, crot); play(note_F4, minim); rest(crot);

}

}


void ding_dong_merrily() {

set_tempo(default_tempo);

for (uint8_t repeat = 1; repeat <= 2; repeat++) {

play(note_G4, crot); play(note_G4, crot); play(note_A4, quav); play(note_G4, quav);

play(note_FS4, quav); play(note_E4, quav); play(note_D4, minim); rest(crot);

play(note_D4, crot); play(note_E4, crot); play(note_G4, crot); play(note_G4, crot);

play(note_FS4, crot); play(note_G4, minim); play(note_G4, crot); rest(crot);

}

}


void good_king_wenceslas() {

set_tempo(vivace);

for (uint8_t repeat = 1; repeat <= 2; repeat++) {

rest(minim); play(note_G4, crot); play(note_G4, crot); play(note_G4, crot);

play(note_A4, crot); play(note_G4, crot); play(note_G4, crot); play(note_D4, minim);

play(note_E4, crot); play(note_D4, crot); play(note_E4, crot); play(note_FS4, crot);

play(note_G4, minim); play(note_G4, minim);

}

}


void the_first_nowell() {

set_tempo(default_tempo);

for (uint8_t repeat = 1; repeat <= 2; repeat++) {

rest(minim); play(note_FS4, quav); play(note_E4, quav); play(note_D4, crot + quav);

play(note_E4, quav); play(note_FS4, quav); play(note_G4, quav); play(note_A4, minim);

play(note_B4, quav); play(note_CS5, quav); play(note_D5, crot); play(note_CS5, crot);

play(note_B4, crot); play(note_A4, minim);

}

}


void the_holly_and_the_ivy() {

set_tempo(default_tempo);

for (uint8_t repeat = 1; repeat <= 2; repeat++) {

rest(minim); play(note_G4, crot); play(note_G4, quav); play(note_G4, quav);

play(note_G4, crot); play(note_E5, crot); play(note_D5, crot); play(note_B4, crot + quav);

play(note_G4, quav); play(note_G4, quav); play(note_G4, quav); play(note_G4, crot);

play(note_E5, crot); play(note_D5, minim); rest(crot);

}

}


void we_three_kings() {

set_tempo(default_tempo);

for (uint8_t repeat = 1; repeat <= 2; repeat++) {

play(note_B4, crot); play(note_A4, quav); play(note_G4, crot); play(note_E4, quav);

play(note_FS4, quav); play(note_G4, quav); play(note_FS4, quav); play(note_E4, crot + quav);

play(note_B4, crot); play(note_A4, quav); play(note_G4, crot); play(note_E4, quav);

play(note_FS4, quav); play(note_G4, quav); play(note_FS4, quav); play(note_E4, crot + quav);

rest(crot + quav);

}

}


void silent_night() {

set_tempo(default_tempo);

rest(crot);

for (uint8_t repeat = 1; repeat <= 2; repeat++) {

play(note_G4, crot + quav); play(note_A4, quav); play(note_G4, crot); play(note_E4, dot_minim);

play(note_G4, crot + quav); play(note_A4, quav); play(note_G4, crot); play(note_E4, dot_minim);

play(note_D5, minim); play(note_D5, crot); play(note_B4, dot_minim);

play(note_C5, minim); play(note_C5, crot); play(note_G4, dot_minim);

}

}



These are the 9 songs;

we_wish_you_a_merry_christmas();

o_come_all_ye_faithful();

away_in_a_manger();

ding_dong_merrily();

good_king_wenceslas();

the_first_nowell();

the_holly_and_the_ivy();

we_three_kings();

silent_night();



Conclusion

oled and piezo1.jpg
OLED and piezo jpg.jpg

This Instructables shows how to use an OLED and piezo with Arduino to play Christmas songs .I hope this helps you to understand this .Thank you




https://vimeo.com/manage/videos/1225413465


https://vimeo.com/manage/videos/1225412795