0% found this document useful (0 votes)
74 views

Arduino Project Coding

The document describes 11 projects involving digital and analog input/output with Arduino. Project 1-4 involve digital input/output like blinking LEDs and a traffic light simulation. Project 5-6 involve analog input/output like pulse width modulation to dim an LED or buzzer. Project 7-9 involve analog to digital conversion like reading sensor values and displaying on serial monitor. Project 10-11 involve interfacing with an LCD display like showing text or temperature readings.

Uploaded by

Desan Scientist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Arduino Project Coding

The document describes 11 projects involving digital and analog input/output with Arduino. Project 1-4 involve digital input/output like blinking LEDs and a traffic light simulation. Project 5-6 involve analog input/output like pulse width modulation to dim an LED or buzzer. Project 7-9 involve analog to digital conversion like reading sensor values and displaying on serial monitor. Project 10-11 involve interfacing with an LCD display like showing text or temperature readings.

Uploaded by

Desan Scientist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

DIGITAL INPUT/OUTPUT

Project 1: Blinking LED

void setup ()
{
pinMode(13, OUTPUT);
}

void loop ()
{
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}

Project 2: Blinking 4 LED

Project 3: Interactive Traffic Light

int carRed = 12;


int carYellow = 11;
int carGreen = 10;
int pedRed = 9;
int pedGreen = 8;
int button = 2;
int crossTime = 5000;
unsigned long changeTime;

void setup ()
{
pinMode(carRed, OUTPUT);
pinMode(carYellow, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(button, OUTPUT);

digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);

void loop ()
{
int state = digitalRead(button);
if(state == HIGH && (millis() - changeTime) > 5000)
{
changeLights();
}
}

void changeLights ()
{
digitalWrite(carGreen, LOW);
digitalWrite(carYellow, HIGH);
delay(2000);

digitalWrite(carYellow, LOW);
digitalWrite(carRed, HIGH);
delay(1000);

digitalWrite(pedRed, LOW);
digitalWrite(pedGreen, HIGH);
delay(crossTime);

for (int x=0; x<10; x++)


{
digitalWrite(pedGreen, HIGH);
delay(250);
digitalWrite(pedGreen, LOW);
delay(250);

digitalWrite(pedRed, HIGH);
delay(500);

digitalWrite(carYellow, HIGH);
digitalWrite(carRed, LOW);
delay(1000);
digitalWrite(carGreen, HIGH);
digitalWrite(carYellow, LOW);

changeTime = millis();
}

Project 4: RGB LED

void setup ()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
}

void loop ()
{
digitalWrite(13, HIGH);
delay(300);
digitalWrite(13, LOW);
delay(300);

digitalWrite(12, HIGH);
delay(300);
digitalWrite(12, LOW);
delay(300)

digitalWrite(11, HIGH);
delay(300);
digitalWrite(11, LOW);
delay(300);

//change HIGH->LOW and LOW->HIGH for common anode RGB

ANALOG INPUT/OUTPUT
1. PWM

Project 5: Pulsing LED/Light dimmer

int i=0;
void setup()
{
pinMode(9, OUTPUT);
}

void loop()
{
for(i; i<255; i++)
{
analogWrite(9, i);
delay(100);
}

for(i=255; i>0; i--)


{
analogWrite(9, i);
delay(100);
}
}

Project 6: Pulsing buzzer

int speakerPin = 9;
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

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' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

// play the tone corresponding to the note name


for (int i = 0; i < 8; i++)
{
if (names[i] == note)
{
playTone(tones[i], duration);
}
}
}

void setup()
{
pinMode(speakerPin, OUTPUT);
}

void loop()
{
for (int i = 0; i < length; i++)
{
if (notes[i] == ' ')
{
delay(beats[i] * tempo); // rest
}

else
{
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}

2. ADC AND SERIAL COMMUNICATION

Project 7: Potentionmeter

int value = 0;

void setup ()
{
Serial.begin(9600);

void loop ()
{
value = analogRead(A0);

Serial.println(value);
delay(500);

Project 8: LED chase effect

// Create array for LED pins


byte ledPin[] = {4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int ledDelay; // delay between changes
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int potPin = 2; // select the input pin for the potentiometer
void setup()
{
// set all pins to output
for (int x=0; x<10; x++) {
pinMode(ledPin[x], OUTPUT); }
changeTime = millis();
}
void loop() {
// read the value from the pot
ledDelay = analogRead(potPin);
// if it has been ledDelay ms since last change
if ((millis() - changeTime) > ledDelay)
{
changeLED();
changeTime = millis();
}
}
void changeLED()
{
// turn off all LED's
for (int x=0; x<10; x++)
{
digitalWrite(ledPin[x], LOW);
}
// turn on the current LED
digitalWrite(ledPin[currentLED],HIGH);
// increment by the direction value
currentLED += direction;
// change direction if we reach the end
if (currentLED == 9)
{direction = -1;}
if (currentLED == 0)
{direction = 1;}
}

Project 9: Temperature sensor reading

int value = 0;
float voltage = 0;
float temp = 0;

void setup ()
{
Serial.begin(9600);
}

void loop ()
{
value = analogRead(A0);
voltage = value*0.00488;
temp = voltage*100;
Serial.println(temp);
delay(500);
}

3. LCD interface

Project 10: LCD display

#include <LiquidCrystal.h>
LiquidCrystal my_lcd(12,11,10,9,8,7);

void setup()
{
my_lcd.begin(16,2);
}

void loop()
{
my_lcd.setCursor(0,0);

my_lcd.print(“ARDUINO TRAINING”);

Project 11: Thermometer LCD display

#include <LiquidCrystal.h>
LiquidCrystal my_lcd(12,11,10,9,8,7);

int value = 0;
float voltage = 0;
float temp = 0;

void setup()
{
my_lcd.begin(16,2);
}

void loop()
{
value = analogRead(A0);
voltage = value*0.00488;
temp = voltage*100;
my_lcd.setCursor(0,0);

my_lcd.print("temp = ");
my_lcd.print(temp);

delay(100);
}

You might also like