Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Sketch 1: Blink (led 13)

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

void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

Sketch 2: Blink (led 2)


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

void loop() {
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(2, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

Sketch 3: Led and switch - simple


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

void loop() {
digitalWrite(2, HIGH); // set the LED on
}
Sketch 4: Led and switch - advanced
void setup() {
pinMode(2, OUTPUT);
pinMode(4, INPUT_PULLUP);
}

void loop() {
int sensorValue = digitalRead(4);
if(sensorValue) {
digitalWrite(2, LOW); // set the LED off
} else {
digitalWrite(2, HIGH); // set the LED on
}
}

Sketch 5: Fading led - automatic


int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

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

void loop() {
// set the brightness of pin 3:
analogWrite(3, 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 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
Sketch 6: Fading led – with potentiometer
int potPin = 5;
int potValue;
int led = 3;
int ledValue;

void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}

void loop() {
potValue = analogRead(potPin);
ledValue = (255.0 / 1023.0)* potValue;
analogWrite(led, ledValue);
Serial.print(String("Pot Value=") + potValue);
Serial.println(String(" Led Value=") + ledValue);
delay(50);
}

Sketch 7: Buzzer

void setup() {
// initialize the digital pin as an output.
pinMode(7, OUTPUT);
}

void loop() {
digitalWrite(7, HIGH); // make the Buzzer sound
delay(1000); // wait for a second
digitalWrite(7, LOW); // set the Buzzer off
delay(1000); // wait for a second
}
Sketch 8: LDR
void setup() {
Serial.begin(9600);
pinMode(2,OUTPUT);
}

void loop() {
int sensorValue = analogRead(3);
Serial.print(sensorValue);
if(sensorValue>500)
{
digitalWrite(2,HIGH);
}
else
digitalWrite(2,LOW);
}

Sketch 9: Temperature
float powervoltage=5;

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

void loop() {
float temperature;
float sensorValue = analogRead(0);
temperature=(sensorValue/1024.0)*powervoltage*100;
Serial.print("The temperature degree is:");
Serial.println(temperature,1);
delay(1000);
}
Sketch 10: RGB led
int red = 3;
int blue = 5;
int green = 6;

void setup() {
pinMode(red, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
}

void loop() {
int i,j,k=0;
for(i=0;i<10;i++)
{
analogWrite(red, i*25);
for(j=0;j<10;j++)
{
analogWrite(blue, j*25);
for(k=0;k<10;k++)
{
analogWrite(green, k*25);
delay(10);
}
}
}
}

Sketch 11: Servo and Pot


#include <Servo.h>
Servo myservo;
int potPin = 5;
int servoPin = 3;
int val;

void setup()
{
myservo.attach(servoPin);
}
void loop()
{
val = analogRead(potPin);
val = map(val, 0, 1023, 0, 179);
myservo.write(val);
delay(15);
}

You might also like