Evans Manyara
Published © GPL3+

Digital Inputs and Outputs

Illustrates how to read digital inputs like a button switch, and how to control a digital output, like an LED.

IntermediateProtip1 hour23
Digital Inputs and Outputs

Things used in this project

Story

Read more

Schematics

SChematics

Code

Controlling LED with a pushbutton

Arduino
// how to read digital inputs like a button switch, and 
// how to control a digital output, like an LED
const int buttonPin = 4;
const int ledPin = 16;
int buttonState =0;
void setup(){
  Serial.begin(115200);

  pinMode(buttonPin, INPUT);

  pinMode(ledPin, OUTPUT);
}
void loop(){
  // read state off thr pushbutton value
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);
  if(buttonState == HIGH){
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
  }
  else{
    digitalWrite(ledPin, LOW);
  }
}

Credits

Evans Manyara
1 project • 0 followers

Comments