Sunday, 26 November 2023
Hybrid transistors set stage for integration of biology and microelectronics
Saturday, 25 November 2023
First experimental evidence of hopfions in crystals opens up new dimension for future technology
Thursday, 23 November 2023
Revolutionary breakthrough in the manufacture of photovoltaic cells
Wednesday, 22 November 2023
Gold now has a golden future in revolutionizing wearable devices
Tuesday, 14 November 2023
Arduino Sound Sensor: Control an LED with Sound
In this tutorial, you’ll learn how to connect an Arduino sound sensor to control an LED with sound. By the end of this guide, you’ll have a working sound-activated LED!
Parts List
- Arduino
- Sound Sensor Module (for example VMA309)
- Light-Emitting Diode (LED)
- 220Ω Resistor (R1)
- Breadboard
- Jumper Wires
How To Connect the Arduino Sound Sensor
In the schematic diagram below, you can see the complete connections for this project.
Connect the sound sensor’s DO (Digital Output) pin to pin 2 on the Arduino. The AO (Analog Output) is not used in this example. The LED’s anode (longer leg) goes through a 220Ω resistor to pin 3 on the Arduino, and the cathode (shorter leg) goes to the GND (Ground).
Breadboard Illustration
Check out the breadboard layout image below. It shows how to place the components on the breadboard and connect them with jumper wires. Ensure that the LED is oriented correctly with the longer leg connected through the resistor.
Arduino Sound Sensor Example Code
Upload the following code to your Arduino UNO:
// Define the pins used
int micPin = 2;
int ledPin = 3;
// Variables for holding the mic value and led state
int micValue;
int ledState
void setup()
{
pinMode(micPin, INPUT); // Configures the sound sensor pin as input
pinMode(ledPin, OUTPUT); // Configures the LED pin as output
}
void loop()
{
// Read the sound sensor value
micValue = digitalRead(micPin);
// Check if the sound sensor has detected noise
if (micValue == HIGH)
{
ledState = digitalRead(ledPin); // Retrieves the current LED state
digitalWrite(ledPin, !ledState); // Toggles the LED state
delay(500); // Pauses for 0.5 seconds
}
}
Step-by-Step Instructions
- Assemble your circuit as per the schematic and breadboard illustrations.
- Connect your Arduino UNO to your computer with the USB cable.
- Open the Arduino IDE and copy the example code into a new sketch.
- Select the correct board and port under the Tools menu.
- Upload the sketch to your Arduino UNO.
- Test the setup by making a noise near the sound sensor. The LED should toggle on and off with each detected sound.
Once you’ve uploaded the code to your Arduino, making a sound near the sensor should cause the LED to toggle. It will switch on if it was off, and off if it was on.
Common Issues and Solutions
If your Arduino Sound Sensor isn’t working, try this:
- LED not responding: Ensure the LED is correctly placed according to its polarity and that all connections are secure.
- No reaction to sound: Adjust the sensitivity of the sound sensor, if possible, or check the connections to pin 2.
Conclusion
You now have a sound-activated LED switch! If you have any questions, drop a comment below or reach out via the contact form.
Copyright Build Electronic Circuits
Arduino Speaker Tutorial: Play Melodies with Your Arduino
In this beginner-friendly tutorial, you’ll learn how to set up an Arduino Speaker circuit where you will play a melody from your Arduino. By the end, you’ll have a basic understanding of how to create Arduino projects that play melodies.
What You’ll Need:
- Arduino
- Speaker (or buzzer)
- Breadboard
- Jumper wires
How To Connect A Speaker to Arduino
Check out the schematic diagram below to see how you can connect a speaker to your Arduino. Connect one wire of the speaker to digital pin 8 on the Arduino, and the other terminal to the GND (ground) pin.
Should You Connect a Speaker Directly to Arduino?
Note that it’s not recommended to connect a speaker directly to an Arduino pin. For example, if you connect an 8-ohm speaker directly between a digital pin and GND, you’ll get 5V across the speaker whenever the pin is high.
Using Ohm’s law, you can calculate the current (I) that the speaker would try to pull out from the Arduino:
For a resistor of 8 Ω with a voltage of 5 V across it, the current flowing through the resistor is:
So the speaker will try to pull out 625 mA, which is way more than the maximum 20 mA that an Arduino can provide.
This could potentially lead to damaging the pin and/or board. That said, many (including me) have tried connecting a speaker directly without any noticeable damage. So if you want to do a quick test, go for it. But if you want to make a permanent speaker circuit, you should either use a transistor to drive the speaker or replace the speaker with a buzzer.
Connecting the Arduino Speaker Circuit
Check out the below image for a breadboard illustration of the setup. Make sure your speaker is connected properly to pin 8 and GND on the breadboard.
You don’t really need a breadboard for this though, you could connect the speaker directly into the pin headers on your Arduino board.
Arduino Speaker Example Code
The code uses the tone() function in Arduino to play tones. This function has three parameters; pin, frequency, and duration. You define your melody through the array melody[], and you use the array noteDurations[] to control the timing of each note.
If you place a 0
in the melody array, it represents a rest or pause in the melody. The corresponding duration value in noteDurations
determines the length of this pause.
Upload the following code to your Arduino. This code will play a simple melody through the speaker.
// Define the buzzer pin
int buzzerPin = 8;
// Define the "Happy Birthday" melody
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, // "Happy Birthday to You"
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4, // "Happy Birthday to You"
NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_E4, NOTE_D4, // "Happy Birthday dear [Name]"
NOTE_AS4, NOTE_AS4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4 // "Happy Birthday to You"
};
// Define the note durations
int noteDurations[] = {
4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 2
};
// Note definitions for the melody
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_C5 523
void setup() {
// Iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 24; thisNote++) {
// To calculate the note duration, take one second divided by the note type.
int noteDuration = 1000 / noteDurations[thisNote];
tone(buzzerPin, melody[thisNote], noteDuration);
// To distinguish the notes, set a minimum time between them.
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// Stop the tone playing:
noTone(buzzerPin);
}
}
void loop() {
// No need to repeat the melody in the loop for this example.
// The setup() is enough to play it once.
}
Instructions:
- Assemble your circuit as shown in the breadboard illustration.
- Connect your Arduino UNO to your computer using a USB cable.
- Open the Arduino IDE on your computer.
- Copy and paste the example code provided above into a new sketch.
- Verify and upload the sketch to your Arduino UNO.
- Once uploaded, the melody should start playing through the speaker.
Create Your Own Melodies
In the code above, we only defined the frequencies of the notes that we needed for the melody. If you want to create other melodies, you might need other notes. Copy the code below and save it as pitches.h, then include this file in your code to have all notes available.
#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
More Songs with Code Examples
When you have included the pitches.h file above, you can play a lot of different songs. Just update the arrays melody and noteDuration in the example code above. Below, I’ve listed a few common songs:
Jingle Bells
int melody[] = {
NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4,
NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_G4
};
int noteDurations[] = {
8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 2, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 8, 8, 8, 2
};
Mary Had a Little Lamb
int melody[] = {
NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_G4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_C4
};
int noteDurations[] = {
4, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 2
};
Twinkle Twinkle Little Star
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4
};
int noteDurations[] = {
4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2
};
Star Wars Main Theme
int melody[] = {
NOTE_A4, 0, NOTE_A4, 0, NOTE_A4, 0, NOTE_F4, NOTE_C5, NOTE_A4, 0, NOTE_F4, NOTE_C5, NOTE_A4, 0, NOTE_E5, 0, NOTE_E5, 0, NOTE_E5, 0, NOTE_F5, NOTE_C5, NOTE_GS4, NOTE_F4, NOTE_C5, NOTE_A4, 0
};
int noteDurations[] = {
500, 500, 500, 500, 500, 500, 350, 150, 500, 500, 350, 150, 650, 500,
500, 500, 500, 500, 500, 350, 150, 500, 500, 350, 150, 650, 500
};
By following these steps, you should have a functioning musical Arduino project. Feel free to experiment with different melodies and note durations. And if you have any questions or suggestions, please leave a comment below!
Copyright Build Electronic Circuits
Monday, 13 November 2023
Arduino Buzzer Tutorial: Play Melodies with Your Arduino
In this quickstart tutorial, you’ll learn how to control a passive buzzer using your Arduino UNO to play a simple melody. By the end, you’ll have a basic understanding of how to incorporate sound into your Arduino projects.
Materials Needed
- Arduino
- Passive Buzzer
- Breadboard
- Jumper Wires
Arduino Buzzer Schematic Diagram
Refer to the schematic diagram provided to connect your passive buzzer to digital pin 8 on the Arduino board.
Breadboard Layout
This is a very simple circuit to connect.
- Connect one lead of the passive buzzer to the digital pin 8 on the Arduino.
- Connect the other lead to a ground (GND) pin on the Arduino.
Check out the breadboard image below to see how you can connect this circuit to a breadboard.
Arduino Buzzer Example Code
The code uses the tone() function in Arduino to play tones. The function has three parameters; pin, frequency, and duration. The melody is defined through the array melody[], and the timing is controlled through the array noteDurations[].
A 0
in the melody array represents a rest or pause in the melody, and the corresponding duration values in noteDurations
determine the length of these pauses.
Copy the code below into your Arduino IDE. This code will play a simple melody through the buzzer.
// Define the buzzer pin
int buzzerPin = 8;
// Define the "Happy Birthday" melody
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, // "Happy Birthday to You"
NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4, // "Happy Birthday to You"
NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_E4, NOTE_D4, // "Happy Birthday dear [Name]"
NOTE_AS4, NOTE_AS4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4 // "Happy Birthday to You"
};
// Define the note durations
int noteDurations[] = {
4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 2
};
// Note definitions for the melody
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_C5 523
void setup() {
// Iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 24; thisNote++) {
// To calculate the note duration, take one second divided by the note type.
int noteDuration = 1000 / noteDurations[thisNote];
tone(buzzerPin, melody[thisNote], noteDuration);
// To distinguish the notes, set a minimum time between them.
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// Stop the tone playing:
noTone(buzzerPin);
}
}
void loop() {
// No need to repeat the melody in the loop for this example.
// The setup() is enough to play it once.
}
Testing and Troubleshooting
- After uploading the code, the buzzer should play the melody.
- If you don’t hear any sound, check your connections and ensure the buzzer is functional.
- Verify that you have selected the correct board and port in the Arduino IDE before uploading.
Define Your Own Melody
In the code above, we only defined the frequencies of the notes that we needed for the melody. If you want to create other melodies, you might need other notes. Copy the code below and save it as pitches.h, then include this file in your code to have all notes available.
#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
Typical Songs Code Examples
When you have included the pitches.h file above, you can play a lot of different songs. Just update the arrays melody and noteDuration in the example code above. Below, I’ve listed a few common songs:
Jingle Bells
int melody[] = {
NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4,
NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_G4
};
int noteDurations[] = {
8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 2, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 8, 8, 8, 2
};
Mary Had a Little Lamb
int melody[] = {
NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_G4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_C4
};
int noteDurations[] = {
4, 4, 4, 4, 4, 4, 2, 4, 4, 2, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 2
};
Twinkle Twinkle Little Star
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4
};
int noteDurations[] = {
4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2
};
Star Wars Main Theme
int melody[] = {
NOTE_A4, 0, NOTE_A4, 0, NOTE_A4, 0, NOTE_F4, NOTE_C5, NOTE_A4, 0, NOTE_F4, NOTE_C5, NOTE_A4, 0, NOTE_E5, 0, NOTE_E5, 0, NOTE_E5, 0, NOTE_F5, NOTE_C5, NOTE_GS4, NOTE_F4, NOTE_C5, NOTE_A4, 0
};
int noteDurations[] = {
500, 500, 500, 500, 500, 500, 350, 150, 500, 500, 350, 150, 650, 500,
500, 500, 500, 500, 500, 350, 150, 500, 500, 350, 150, 650, 500
};
I hope this tutorial was helpful. If you have any questions or suggestions, please leave a comment below!
Copyright Build Electronic Circuits
Tiny, wireless antennas use light to monitor cellular communication
Researchers developed a biosensing technique that eliminates the need for wires. Instead, tiny, wireless antennas use light to detect minute...
-
In this project, we will learn about the MCP2515 CAN Controller Module, how to interface the MCP2515 CAN Bus Controller with Arduino and fin...
-
Interfacing DC motor to the microcontroller is a very important concept in many industrial and robotic applications. By interfacing DC motor...
-
Do you need a MOSFET gate resistor? What value should it be? And should it go before or after the pulldown resistor? If you’re a bit impati...