Day 2- Arduino PWM, LED Brightness Control, LED fade

 Arduino Programming Training Program - Day 2

Task

1) Control LED using Push button

2) Control LED brightness using  Push button

3) Control RGB LED using Push button

4) Control 12 Volt RGB LED strip using Push button

5) Generate Multiple Colors on 12 Volt LED strip

6) Control Servo Moter using Push button

7) Control Servo Motor using POT

8) Automatic Door using Arduino

9) Automatic Dustbin using Arduino

10) Anti Theft alarm uisng Arduino






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

Connect three wires to the board. The first two, red and black, connect to the two long vertical rows on the side of the breadboard to provide access to the 5 volt supply and ground. The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the button connects through a pull-down resistor (here 10K ohm) to ground. The other leg of the button connects to the 5 volt supply.

When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that we read a HIGH.

You can also wire this circuit the opposite way, with a pullup resistor keeping the input HIGH, and going LOW when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button.

If you disconnect the digital I/O pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit.



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() {

  pinMode(ledPin, OUTPUT);

  pinMode(buttonPin, INPUT);

}


void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {

    digitalWrite(ledPin, HIGH);

  }

 else {

    digitalWrite(ledPin, LOW);

  }

}


------------------------------------------------------------------------------------------------------------------------

LED Fade

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

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

void loop() {
 
  analogWrite(led, brightness);

  brightness = brightness + fadeAmount;

  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  delay(30);
}

--------------------------------------------------------------------------------


How to Read Analog INPUT from Potentiometer


void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // print out the value you read: Serial.println(sensorValue); delay(1); // delay in between reads for stability }

------------------------------------------------------------------------------

How to control LED brightness using Potentiometer


--------------------------------------------------------------------------------------

Control LED using 2 Push Button

int LED1=13; int button1=14; int button2=15; void setup() { pinMode(LED1, OUTPUT); digitalWrite(LED1,HIGH);; pinMode(button1,INPUT); pinMode(button2,INPUT); } void loop() { byte sw1= digitalRead(button1); byte sw2= digitalRead(button2); if (sw1==0){digitalWrite(LED1,LOW); delay(500);} if (sw2==0){digitalWrite(LED1,HIGH); delay(500);} }

-----------------------------------------------------------------------------

Servo Motor using POT



Servo myservo; // create servo object to control a servo int potpin = A0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it for use with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there }

-----------------------------------------------------------------------------------

//IR remote with AC switch

#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

const int buttonPin = 2;  // the number of the pushbutton pin
const int ledPin = 13;    // the number of the LED pin
byte LEDstate=1;
byte previousState=1;

// variables will change:
int buttonState = 0;  // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin,HIGH);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH && previousState==0) {
     LEDstate=!LEDstate;
     previousState=1;
  }
    if (buttonState == LOW && previousState==1) {
     LEDstate=!LEDstate;
     previousState=0;
  }
////////////////////////////////////////////
  if (irrecv.decode(&results)) {
    Serial.println(results.value, DEC);

if(results.value==16752735) //OK  key remotttttttttttttttttt
 {LEDstate=!LEDstate;}

    irrecv.resume(); // Receive the next value
  }
  delay(100);

////////////////////////////////////////////

   digitalWrite(ledPin, LEDstate);
}


----------------------------------------------------------------

No comments:

Post a Comment

Arduion Uno pin out