Arduino Code for Controlling an LED with a Button

CIRCUIT DIAGRAM:


This is the led turned on  by the push button:
a circuit diagram of an Arduino connected to a led and a push button.the led is turned on



this is the led turned off by the push button:

a circuit diagram of an Arduino connected to a led and a push button.the led is turned off









COMPONENTS:

list of component for the project, arduino,led aqua,button, resistor



CODE:

int ledpin =8;
int buttonpin = 12;
int buttonread ;
int dt =500;
void setup() {
  // put your setup code here, to run once:
pinMode(ledpin,OUTPUT);
pinMode(buttonpin,INPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
buttonread =digitalRead(buttonpin);
Serial.println(buttonread);
delay(dt);
if(buttonread == 1){
digitalWrite(ledpin,HIGH);
}
delay(dt);
if(buttonread == 0){
digitalWrite(ledpin,LOW);
}
}

EXPLANATION OF THE CODE:



int ledpin =8;` - This line initializes an integer variable called `ledpin` with a value of 8. This variable will be used to store the pin number of an LED that is connected to the Arduino board.

`int buttonpin = 12;` - This line initializes another integer variable called `buttonpin` with a value of 12. This variable will be used to store the pin number of a button that is connected to the Arduino board.

`int buttonread ;` - This line declares an integer variable called `buttonread`, which will be used to store the value read from the button.


`int dt =500;` - This line initializes an integer variable called `dt` with a value of 500. This variable will be used to set the delay time between each loop iteration.


`void setup() {` - This line starts the definition of a function called `setup()`, which is executed once when the Arduino board is powered on or reset.


`pinMode(ledpin,OUTPUT);` - This line sets the mode of the `ledpin` as output, which means that it can be used to send signals to an LED.


`pinMode(buttonpin,INPUT);` - This line sets the mode of the `buttonpin` as input, which means that it can be used to read signals from a button.




`Serial.begin(9600);` - This line initializes serial communication at a baud rate of 9600 bits per second.




`}` - This line ends the definition of the `setup()` function.




`void loop() {` - This line starts the definition of a function called `loop()`, which is executed repeatedly as long as the Arduino board is powered on or reset.




`buttonread =digitalRead(buttonpin);` - This line reads the value from the button connected to the `buttonpin` and stores it in the variable called `buttonread`.


`Serial.println(buttonread);` - This line prints the value stored in the variable called `buttonread` to the serial monitor.


`delay(dt);` - This line waits for a delay time equal to the value stored in the variable called `dt`.


`if(buttonread == 1){` - This line starts an if statement that checks if the value stored in the variable called `buttonread` is equal to 1.


`digitalWrite(ledpin,HIGH);` - If step 14 is true, this line sets the value of pin number stored in variable called 'ledpin' as HIGH, which turns on an LED connected to that pin.


`}` - This line ends the if statement started in step 14.

`delay(dt);` - This line waits for a delay time equal to the value stored in the variable called 'dt'.


`if(buttonread == 0){` - This line starts another if statement that checks if the value stored in the variable called 'buttonread' is equal to 0.


`digitalWrite(ledpin,LOW);` - If step 18 is true, this line sets the value of pin number stored in variable called 'ledpin' as LOW, which turns off an LED connected to that pin.


`} - This line ends the if statement started in step 18.


`} - This line ends the definition of function called 'loop().

1 comment:

  1. This content is absolutely fantastic! I gained a wealth of knowledge from it, and I'm truly impressed. Your efforts are commendable – please continue producing such high-quality work. I'm excited to become a loyal follower of your blog moving forward!

    ReplyDelete

Featured

WHAT IS WEBSCRAPING IN PYTHON

  Web scraping is a technique that allows you to extract data from websites and store it in a format of your choice. It can be useful fo...

Powered by Blogger.