ARDUINO PROGRAMS
Built-in LED BLINKING
1:PROGRAM
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
LED BLINKING
Hardware Required
•Arduino Board
•LED
•Resistor
PROGRAM
void setup() {
// set Pin 3 to output
pinMode(3, OUTPUT);
}
void loop() {
digitalWrite(3, HIGH); // turn LED on (output 5V)
delay(1000); // wait one second
digitalWrite(3, LOW); // turn LED off (output 0V)
delay(1000)
}
3: MULTIPLE LED BLINKING
. Three leds blink
The circuit uses three output pins 11, 12, and 13 of the Arduino to control three LEDs blinking.
The connection diagram is similar to that in the example above. You can also refer to the
connection diagram in the simulation video below.
PROGRAM
// the setup function runs once
void setup() {
// initialize digital pin 11, 12, 13 as an output.
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
// turn the LED on
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
delay(1000); // wait for a second
// turn the LED off
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
delay(1000); // wait for a second
digitalWrite(11,LOW);
digotalWrite(12,LOW);
digitalWrite(13,HIGH);
}
4:Control LED Using LDR
PROGRAM
//set pin numbers
//const won't change
const int ledPin = 12; //the number of the LED pin
const int ldrPin = A0; //the number of the LDR pin
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); //initialize the LED pin as an output
pinMode(ldrPin, INPUT); //initialize the LDR pin as an input
}
void loop() {
int ldrStatus = analogRead(ldrPin); //read the status of the LDR value
Serial.println(ldrStatus);
if (ldrStatus <=80) {
digitalWrite(ledPin, HIGH); //turn LED on
}
else {
digitalWrite(ledPin, LOW); //turn LED off
}
}
5:BUZZER PROGRAM
PROGRAM
const int buzzer = 9; //buzzer to arduino pin 9
void setup(){
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
}
void loop(){
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(1000); // ...for 1sec
}
BIRTHDAY SONG USING BUZZER
PROGRAM
int speakerPin = 7; // Buzzer pin
int length = 28; // the number of notes
char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc";
int beats[] = {2,2,8,8,8,16,1,2,2,8,8,8,16,1,2,2,8,8,8,8,16,1,2,2,8,8,8,16};
int tempo = 200;// time delay between notes
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B',
'c', 'd', 'e', 'f', 'g', 'a', 'b',
'x', 'y' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014,
956, 834, 765, 593, 468, 346, 224,
655 , 715 };
int SPEE = 5;
// play the tone corresponding to the note name
for (int i = 0; i < 17; i++) {
if (names[i] == note) {
int newduration = duration/SPEE;
playTone(tones[i], newduration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // delay between notes
} else {
playNote(notes[i], beats[i] * tempo);
}
// time delay between notes
delay(tempo);
}
}