Skip to main content

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

Comments

  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

Post a Comment

Popular posts from this blog

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 for various purposes, such as market research, price comparison, content analysis, and more. In this blog post, i will show you everything a beginner needs to know about web scraping, from the basics to some advanced tips and tricks.   What is web scraping?   Web scraping is the process of programmatically retrieving information from web pages. It involves sending requests to web servers, parsing the html code of the web pages, and extracting the data you want. Web scraping can be done manually, by copying and pasting data from a website, or automatically, by using a software tool or a programming language.   Why web scrape?   Web scraping can help you access data that is not available through an api or a downloadable file. For example, you may want to scrape product reviews from an e-commerce website, or news ...

Thevenin’s Theorem: A Beginner’s Guide

                                        table of content  Introduction History of Thevenin’s theorem Basic circuit analysis concepts Voltage, current, and resistance Ohm’s law Thevenin’s theorem: principles and applications Statement of the theorem Finding the Thevenin equivalent circuit What is Thevenin’s theorem? When should you use Thevenin’s theorem? How do you apply Thevenin’s theorem to a circuit Conclusion . Introduction Electrical engineers can effectively convert complicated circuits into smaller equivalent circuits by using Thevenin's theorem. The French telegraph engineer Léon Charles Thévenin is honored by having his theorem called in his honor. He proposed it in 1883. History of Thevenin’s theorem Hermann von Helmholtz, a German scientist, independently derived Thevenin's theorem in 1853; Léon Charles Thévenin did ...

Study Linear Regression: A Beginner’s Guide with c++

Table of content: • Introduction • What is Linear Regression? • Simple vs. Multiple Linear Regression • Implementing Linear Regression in C++ • Complex Linear Regression • Conclusion • References Understanding Linear Regression: A Beginner’s Guide Linear regression is a powerful statistical tool that allows us to understand the relationship between two or more variables. It is widely used in many fields, including finance, economics, and engineering, to make predictions and analyze data. In this article, we will explore the basics of linear regression, including what it is, how it works, and how to implement it in C++. We will also discuss the differences between simple and multiple linear regression and provide examples to help you understand these concepts. What is Linear Regression? At its core, linear regression is a method for finding the line of best fit that describes the relationship between two continuous variables. This line can be used to make predictions about o...