Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO GSM-Controlled Melody Player

View and Edit Project In Cirkit DesignerImage of Arduino UNO GSM-Controlled Melody Player

Circuit Documentation

Summary of the Circuit

This circuit consists of an Arduino UNO microcontroller, a GSM SIM900 module, and a Piezo Speaker. The Arduino UNO is used as the central processing unit, controlling the GSM module for SMS communication and driving the Piezo Speaker to play a melody when a specific SMS message is received. The GSM module is responsible for cellular communication, allowing the circuit to send and receive SMS messages. The Piezo Speaker is an output device that produces sound in response to an electrical signal from the Arduino UNO.

Component List

Arduino UNO

Piezo Speaker

GSM SIM900

Wiring Details

Arduino UNO

Piezo Speaker

GSM SIM900

Documented Code

/*
 * This Arduino Sketch communicates with a GSM module to receive an SMS message.
 * When a specific message is received, it triggers the piezo speaker connected
 * to pin D8 and GND of the Arduino UNO to play a melody.
 */

#include <SoftwareSerial.h>

// Pin connected to the piezo speaker
const int speakerPin = 8;

// GSM module pins
const int rxPin = 0;
const int txPin = 1;

// Create a software serial port for GSM communication
SoftwareSerial gsmSerial(rxPin, txPin);

// Notes of the melody (frequencies in Hz)
int melody[] = {
  262, 196, 196, 220, 196, 0, 247, 262
};

// Note durations: 4 = quarter note, 8 = eighth note, etc.
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {
  // Initialize serial communication with the GSM module
  gsmSerial.begin(9600);
  // Wait for the GSM module to initialize
  delay(1000);
  // Set the GSM module to text mode
  gsmSerial.println("AT+CMGF=1");
  delay(100);
  // Set the GSM module to show new messages
  gsmSerial.println("AT+CNMI=1,2,0,0,0");
  delay(100);
}

void loop() {
  // Check if there is any data from the GSM module
  if (gsmSerial.available()) {
    String message = gsmSerial.readString();
    // Check if the message contains the trigger word
    if (message.indexOf("PLAY") >= 0) {
      playMelody();
    }
  }
}

void playMelody() {
  // Iterate over the notes of the melody
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    // Calculate the note duration
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(speakerPin, melody[thisNote], noteDuration);
    // Pause for the note's duration plus 30% to distinguish notes
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // Stop the tone playing
    noTone(speakerPin);
  }
  // Add a delay before repeating the melody
  delay(1000);
}

This code is designed to run on the Arduino UNO microcontroller. It sets up a software serial connection to communicate with the GSM SIM900 module. Upon receiving an SMS containing the word "PLAY", the Arduino triggers the Piezo Speaker to play a predefined melody. The melody and its timing are defined by arrays, and the playMelody function controls the duration and pitch of each note played by the speaker.