Skip to main content

Latest Post

What is Industry 4.0?

  What is Industry 4.0 and what are some of the technologies that are driving it? Industry 4.0 is a term that refers to the fourth industrial revolution, which is characterized by the integration of digital technologies, such as artificial intelligence, cloud computing, big data, the internet of things, robotics, and 3D printing, into the manufacturing sector. Industry 4.0 aims to create smart factories that are more efficient, flexible, and responsive to customer needs and market changes. Some of the technologies that are enabling Industry 4.0 are: - Artificial intelligence (AI) : AI is the ability of machines to perform tasks that normally require human intelligence, such as reasoning, learning, decision-making, and problem-solving. AI can help optimize production processes, improve product quality, reduce costs, and enhance customer satisfaction. - Cloud computing: Cloud computing is delivering computing services, such as servers, storage, databases, software, and analytics, over t

Play a melody with a Piezo speaker

 Play a Melody using the tone() function

This example shows how to use the tone() command to generate notes. It plays a little melody you may have heard before.

Hardware Required

  • Arduino board

  • piezo buzzer or a speaker

  • hook-up wires

Making header file

To make the pitches.h file, either click on the button just below the serial monitor icon and choose "New Tab", or use Ctrl+Shift+N.


Then paste in the following code:

/*************************************************

 * Public Constants

 *************************************************/

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#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_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
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

and save it as pitches.h

Code

The code below uses an extra file, pitches.h. This file contains all the pitch values for typical notes. For example, NOTE_C4 is middle C. NOTE_FS4 is F sharp, and so forth. This note table was originally written by Brett Hagman, on whose work the tone() command was based. You may find it useful whenever you want to make musical notes.

The main sketch is as follows:

#include "pitches.h"

// notes in the melody:
int melody[] = {

  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

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

  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {

  // iterate over the notes of the melody:

  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.

    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

    int noteDuration = 1000 / noteDurations[thisNote];

    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.

    // the note's duration + 30% seems to work well:

    int pauseBetweenNotes = noteDuration * 1.30;

    delay(pauseBetweenNotes);

    // stop the tone playing:

    noTone(8);

  }
}

void loop() {

  // no need to repeat the melody.
}

Comments

Popular

Playing with Buttons

Button Pushbuttons or switches connect two points in a circuit when you press them. This example turns on the built-in LED on pin 13 when you press the button. Hardware Arduino Board Momentary button or Switch 10K ohm resistor hook-up wires breadboard Circuit diagram Code // constants won't change. They're used here to // set pin numbers: const int buttonPin = 2 ;     // the number of the pushbutton pin const int ledPin =   13 ;      // the number of the LED pin // variables will change: int buttonState = 0 ;         // variable for reading the pushbutton status void setup () {   Serial . begin ( 9600 );   // initialize the LED pin as an output:   pinMode (ledPin, OUTPUT);   // initialize the pushbutton pin as an input:   pinMode (buttonPin, INPUT); } void loop () {   // read the state of the pushbutton value:   buttonState = digitalRead (buttonPin);   // Show the state of pushbutton on serial monitor   Serial . println (buttonState);   // check if the pushbutton is p

Turn LED On and Off Through LDR

  LDR An LDR ( Light Dependent Resistor ) is a component that has a (variable) resistance that changes with the light intensity that falls upon it. This allows them to be used in light sensing circuits. A photoresistor is made of a high resistance semiconductor. Hardware Required Arduino Board LED 220 ohm resistor LDR 10k ohms resistor Circuit Diagram Code int ldr=A0; //Set A0(Analog Input) for LDR. int value= 0 ; void setup () { Serial . begin ( 9600 ); pinMode ( 3 ,OUTPUT); } void loop () { value= analogRead (ldr); //Reads the Value of LDR(light). Serial . println ( "LDR value is :" ); //Prints the value of LDR to Serial Monitor. Serial . println (value); if (value< 300 )   {     digitalWrite ( 3 ,HIGH); //Makes the LED glow in Dark.   }   else   {     digitalWrite ( 3 ,LOW); //Turns the LED OFF in Light.   } }

Home Automation

A smart system made by using Node MCU dev board. What is Node MCU? NodeMCU is an open-source firmware for which open-source prototyping board designs are available. The name "NodeMCU" combines "node" and "MCU" (micro-controller unit). The term "NodeMCU" strictly speaking refers to the firmware rather than the associated development kits.  Both the firmware and prototyping board designs are open source. Requirments Node MCU 4 channel relay toggle switch * 4 Hi-links (220v ac to 5v dc) Circuit Diagram Program #ifdef ENABLE_DEBUG         #define DEBUG_ESP_PORT Serial         #define NODEBUG_WEBSOCKETS         #define NDEBUG #endif #include <Arduino.h> #include <ESP8266WiFi.h> #include "SinricPro.h" #include "SinricProSwitch.h" #include <map> #define WIFI_SSID         "your wifi name"     #define WIFI_PASS         "your wifi pass" #define APP_KEY           "this code is prov

MQ2 Gas Sensor

 About Gas Sensor The MQ series of gas sensors use a small heater inside with an electrochemical sensor. They are sensitive to a range of gasses and are used indoors at room temperature. The output is an analog signal and can be read with an analog input of the Arduino. The MQ-2 Gas Sensor module is useful for gas leakage detection in homes and industries. It can detect LPG, i-butane, propane, methane, alcohol, hydrogen, and smoke. Some modules have a built-in variable resistor to adjust the sensitivity of the sensor. Note:  The sensor becomes very hot after a while, don't touch it! Required  Arduino UNO Breadboard MQ-2 Gas sensor module Red, Green led 5mm 220 Ohm Buzzer The connections are pretty easy: The MQ-5 sensor Pin-> Wiring to Arduino Uno A0-> Analog pins D0-> none GND-> GND VCC-> 5V other components Pin-> Wiring to Arduino Uno D13-> +ve of buzzer GND-> -ve of buzzer D12-> anode of red light D11-> anode of green light GND-> cathode of red li

Temperature sensor

About the LM35 The LM35 is an inexpensive, precision Centigrade temperature sensor made by  Texas Instruments . It provides an output voltage that is linearly proportional to the Centigrade temperature and is, therefore, very easy to use with the Arduino. The sensor does not require any external calibration or trimming to provide accuracies of ±0.5°C at room temperature and ±1°C over the −50°C to +155°C temperature range. One of the downsides of the sensor is that it requires a negative bias voltage to read negative temperatures. So if that is needed for your project, I recommend using the DS18B20 or TMP36 instead. The TMP36 by Analog Devices is very similar to the LM35 and can read temperatures from -40°C to 125°C without any external components Note that the sensor operates on a voltage range of 4 to 30 V and that the output voltage is independent of the supply voltage. The LM35 is part of a series of analog temperature sensors sold by Texas Instruments. Other members of the series i

Mini Oscilloscope via Arduino Nano

What is an oscilloscope?   An  oscilloscope , formerly known as an oscillograph, is an instrument that graphically displays electrical signals and shows how those signals change over time. It measures these signals by connecting with a sensor, which is a device that creates an electrical signal in response to physical stimuli like sound, light, and heat. For instance, a microphone is a sensor that converts sound into an electrical signal. Oscilloscopes are often used when designing, manufacturing, or repairing electronic equipment. Engineers use an oscilloscope to measure electrical phenomena and solve measurement challenges quickly and accurately to verify their designs or confirm that a sensor is working properly. Scientists, engineers, physicists, repair technicians, and educators use oscilloscopes to see signals change over time. An automotive engineer might use an oscilloscope to correlate analog data from sensors with serial data from the engine control unit. Meanwhile, a medical

Seven Segment Display Interfacing with Arduino

Seven-Segment Introduction  Let’s start the main part of this tutorial by answering a question: what is a seven-segment display? As its name suggests, a 7-segment device consists of 7  light-emitting diodes . These light-emitting diodes are arranged and packed inside a single display with a specific pattern in mind. If this pattern is controlled in a specific way by turning on and turning off LEDs, a seven-segment device will display a unique number. There is also an extra eighth LED on a seven-segment display which is used to display dots. This dot is sometimes used as a decimal point when we want to display a fractional value.  The picture below shows a seven-segment display and its pinout. The string of eight LEDs on the left side shows the internal connection and a picture on the right side shows how these LEDs are arranged to make a seven-segment display. Pin3 and 8 are common pins. These pins are used to provide either 5 volts or ground in common-anode and common cathode type dis

Fade an LED

  Fade This example demonstrates the use of the  analogWrite()  function in fading an LED off and on. AnalogWrite uses  pulse width modulation (PWM) , turning a digital pin on and off very quickly with different ratio between on and off, to create a fading effect. Hardware Required Arduino board LED 220 ohm resistor hook-up wires Circuit Diagram Code int led = 9 ;           // the PWM pin the LED is attached to int brightness = 0 ;    // how bright the LED is int fadeAmount = 5 ;    // how many points to fade the LED by // the setup routine runs once when you press reset: void setup () {   // declare pin 9 to be an output:   pinMode (led, OUTPUT); } // the loop routine runs over and over again forever: void loop () {   // set the brightness of pin 9:   analogWrite (led, brightness);   // change the brightness for next time through the loop:   brightness = brightness + fadeAmount;   // reverse the direction of the fading at the ends of the fade:   if (brightness <= 0 || bri

Blinking LED with Arduino UNO

  Blink This example shows the simplest thing you can do with an Arduino to see physical output: it blinks the on-board LED. Hardware Required Arduino Board LED 220 ohm resistor Circuit diagram Code int ledPin= 13 ; //definition digital 13 pins as pin to control the LED void setup () {     pinMode (ledPin,OUTPUT);    //Set the digital 8 port mode, OUTPUT: Output mode } void loop () {       digitalWrite (ledPin,HIGH); //HIGH is set to about 5V PIN8     delay ( 1000 );               //Set the delay time, 1000 = 1S     digitalWrite (ledPin,LOW);  //LOW is set to about 5V PIN8     delay ( 1000 );               //Set the delay time, 1000 = 1S }