Christmas Carol With OLED and Buzzer

by AkarshA2 in Circuits > Arduino

12 Views, 0 Favorites, 0 Comments

Christmas Carol With OLED and Buzzer

1.JPG

Christmas Time is one of the most amazing times of the year and as we are technical people, we should have our own way to celebrate it. So let's make a project today that will help us to celebrate Christmas in a unique way. Today, we are going to make a project which will play 'We Wish you a Merry Christmas' Carol on Buzzer with Tone() and display a Customized Message with Pine trees on SSD1306 OLED Display.

Its an easy and yet an amazing project to make. You can minimize the whole thing to a PCB board and hang it on your Christmas Tree. We will be using an SSD1306 OLED for the display of the message - "Merry Christmas" with a basic animation of snowflakes. Also, this project is not limited to Christmas only. You can customize it for other occasions or you can also use it as a normal home decor kind of thing. You just need to make some changes in the code according to the tone you want to play and the message you want to display.

Want to know how to do that? Then go ahead and check this out.

Supplies

Things used in this project:-

  • Graphic OLED, 128 x 32 Pixels
  • Arduino Nano R3
  • Buzzer, Piezo
  • Arduino IDE

Get PCBs for Your Projects Manufactured

pcbway.JPG

You must check out PCBWAY for ordering PCBs online for cheap!

You get 10 good-quality PCBs manufactured and shipped to your doorstep for cheap. You will also get a discount on shipping on your first order. Upload your Gerber files onto PCBWAY to get them manufactured with good quality and quick turnaround time. PCBWay now could provide a complete product solution, from design to enclosure production. Check out their online Gerber viewer function. With reward points, you can get free stuff from their gift shop.

About the Components

3.JPG
4.JPG

Before we start let's have a closer look at the components that we are going to use and get an understanding of how they work and what applications they can be used for.

  • Piezo Buzzer: In simplest terms, a piezo buzzer is a type of electronic device that’s used to produce a tone, alarm, or sound. It’s lightweight with simple construction, and it’s typically a low-cost product. Yet at the same time, depending on the piezo ceramic buzzer specifications, it’s also reliable and can be constructed in a wide range of sizes that work across varying frequencies to produce different sound outputs. when certain piezoelectric materials are subjected to an alternating field of electricity, the piezo buzzer element — often a manmade piezoceramic material — stretches and compresses in sequence with the frequency of the current. As a result, it produces an audible sound. Piezo buzzers can typically operate anywhere between three and 250 volts. In addition, they consume less than 30 milliamperes — even at higher rate frequencies
  • OLED Display: There are 2 types of OLED displays based on the mode of communication they use. The names of these displays are SPI-based and I2C-based. These are explained below:-
    1. SPI-based SSD1306: A 7-pin OLED module offers all interfacing options like 3-wire SPI, 4-wire SPI, and I2C. The default 4-wire SPI is the fastest communication mode with the OLED. For interfacing, the module with Arduino connects the GND and VCC/VDD pins to the ground and 5V out of the Arduino, respectively. The D0, D1, RST, DC, and CS pins can be connected to any GPIO. A 6-pin module doesn't have Chip Select (CS) pin.
    2. I2C-based SSD1306: The 4-pin OLED modules offer only an I2C interface to communicate with it. To interface the 4-pin module, connect the GND and VCC pins to this ground and 5V out of the Arduino. Connect the SDA and SCL pins to the pins of Arduino (check for your board's Pinout). I have used a similar OLED, but only the difference is, in the above images the resolution of the OLED is 128x64 (bigger screen). What I used is a smaller height of 32 pixels. Therefore my OLED display was of resolution 128x32

Libraries and Code Setup

6.JPG
5.JPG

In Arduino IDE, Go to Tools > Manage Libraries > Search "SSD1306" on the Library Manager. This library will help to initiate and set up the OLED screen from the preferred Communication easily with the Arduino IDE.

Below code will initiate the library and define the screen resolution (we use) -

#include

#include

#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 32

For SPI,

#define OLED_MOSI 9

#define OLED_CLK 10

#define OLED_DC 11

#define OLED_CS 12

#define OLED_RESET 13

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

For I2C,

#include "Wire.h"

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

Next, we also need to make sure that I2C communication does not fail or if it does then it shows us an exception that the Connection Failed or something like that. So for that, we add the below function inside the setup function:-

void setup() {

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

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

for(;;);

}

}

You can now directly put your display function inside the setup function (print once - static image or text) or inside the loop function (prints seamlessly - dynamic image or text).
Here are some functions that will help to handle the OLED display library to write text or draw simple graphics -

  • display.clearDisplay() – all pixels are off
  • display.drawPixel(x,y, color) – plot a pixel in the x, y coordinates
  • display.setTextSize(n) – set the font size, supports sizes from 1 to 8
  • display.setCursor(x,y) – set the coordinates to start writing text
  • display.print(“message”) – print the characters at location x, y
  • display.display() – call this method for the changes to take effect

For more such functions - https://learn.adafruit.com/adafruit-gfx-graphics-library/graphics-primitives

Now that the basics are over, let us use a much better function, i.e. bitmap for printing images on the OLED screen.

display.drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);

This function draws the bitmap format into the XY plane. Let us take a sample image and convert it to Bitmap. Then apply random snowflakes above it.

Display "Merry Christmas" With Trees - Using BitMap

7.JPG
8.JPG
9.JPG
10.JPG
11.JPG

What we will do for this, is create an image file of the resolution of 128 x 32 pixels. Then convert the image to get a Bitmap Layout of the same. I used Photoshop as it is the ready-to-go platform for editing/creating custom images. After creating one, go to https://javl.github.io/image2cpp/ and do the following:-

Step 1 - Select the image file

Step 2 - Select the appropriate functions and see the LIVE changes in the Preview section.

Step 3 - Click on Generate code to get the bitmap of the image in array format.

Now, that you have the Bitmap array, you can simply assign a variable name and assign the same directly to the bitmap function. That's it. You can now try the same on your Image. Just make sure to select the correct background in Image settings -

Black BG - White FG means LED on those parts will glowWhite BG - Black FG means the Background will glow and the image part will be black. Let us now choose a melody and use it on the Buzzer. The notes of the song "We wish you a Merry Christmas" were converted to tone() to be used with the piezo buzzer.

You can see in the documentation of the function - https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody that usual Notes can be directly used to apply and play a particular song. We accurate delays, it can be put inside a loop for error control.

Now that the basics are clear, you can use the already existing Notes for different songs from - https://github.com/robsoncouto/arduino-songs

And that's it, we are done with the code. You can check the complete code for this project on the Github page of the project which you can access from here.

Connections to Be Done

12.JPG
code.JPG

Now as we are done with the code setup and other important things. The last thing we are left with is the hardware connections and code uploading to the Arduino board. For this project, I am using an I2C-based SSD1306 OLED display. The connections to be done are as given below:-

  • Connect the GND pins of the buzzer and the OLED display to the GND pin of the Arduino Nano.
  • Connect the Vcc pin of the buzzer to the D4 pin of the Arduino Nano.
  • Connect the Vcc pin of the OLED display to the 5V pin of the Arduino Nano.
  • Connect the SDA pin of the display to the A4 pin of the Arduino Nano.
  • Connect the SCL pin of the display to the A5 pin of the Arduino Nano.

And in this way, we are done with the Connections that are needed for this project. Now we need to connect the Arduino Nano to the Computer and upload the code to the Arduino board using Arduino IDE. The Complete code can be accessed from here.

And We Are Done

2.JPG

In this way, the project is complete and ready to be used. You can create the same prototype on a PCB and hang it on your Christmas Tree or anywhere you want. As we said the project was fun and easy to make. Another good thing about this project is that it is completely customizable. You can use it as it is for Christmas or you can change the message and Tone according to your need and use it for other occasions as well. Not only on occasions You can also use it for home decoration.

So that's it for this project. I will be back with some more fun and amazing projects very soon.