In this Arduino tutorial I will show you how to turn an LED on and off with a push button. In fact, we’ll do 2 slightly different applications.

First, we will power on the LED when the button is pressed, and power off the LED when the button is not pressed.

And then we’ll modify the program to toggle the LED’s state only when we release the button.

For more info on each component, also check out this Arduino LED tutorial and this Arduino push button tutorial.

Let’s get started!

>> Watch this video as an additional resource:


You are learning how to use Arduino to build your own projects?

Check out Arduino For Beginners and learn step by step.


After watching the video, subscribe to the Robotics Back-End Youtube channel so you don’t miss the next tutorials!

Arduino circuit with an LED and a button

To build the circuit you will need those components:

  • Arduino board (any board, if you don’t have Uno you can easily adapt by finding corresponding pins).
  • Breadboard.
  • LED – any color.
  • Push button.
  • 220 Ohm resistor for the LED. If you don’t have this specific value, any resistor from 330 to 1k Ohm will do.
  • 10k Ohm resistor for the push button. If you don’t have, you can go until 20k-50k Ohm.
  • A bunch of male to male wires (including if possible black, red, and other colors).

Here’s the circuit you have to make.

Step by step instructions to build the circuit (more info about Arduino pins here):

  • First, make sure to power off your Arduino – remove any USB cable.
  • Plug a black wire between the blue line of the breadboard and a ground (GND) pin on the Arduino board.
  • Plug the LED. You can notice that the LED has a leg shorter than the other. Plug this shorter leg to the ground (blue line here) of the circuit.
  • Connect the longer leg of the LED to a digital pin (here pin no 8, you can change it). Add a 220 Ohm resistor in between to limit the current going through the LED.
  • Add the push button to the breadboard, like in the picture.
  • Connect one leg of the button to the ground, and put a 10k Ohm resistor in between. This resistor will act as a “pull down” resistor, which means that the default button’s state will be LOW.
  • Add a red wire between another leg of the button and VCC (5V).
  • Finally, connect a leg of the button (same side as the pull down resistor) to a digital pin (here 7).

All right your circuit is now finished. You can start writing code.

Turn on the LED when button is pressed, turn it off otherwise

What we want to achieve is simple: when the button is not pressed, the LED is off. And when we press the button the LED should be on.

The code

#define LED_PIN 8
#define BUTTON_PIN 7
 
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
 
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) {
digitalWrite(LED_PIN, HIGH);
}
else {
digitalWrite(LED_PIN, LOW);
}
}

Let’s break this code down line by line.

Setup

#define LED_PIN 8
#define BUTTON_PIN 7

First, as a best practice, we use some defines to keep the pin number for the LED and push button. That way, if you have used different pins than I, you just need to modify those 2 lines. Also, in the future if you want to change the LED from pin 8 to pin 11 for example, you can modify this line without touching anything else in the code.

void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}

The setup function is executed once at the beginning of the program. This is the perfect time to initialize our pins with the pinMode() function:

  • OUTPUT for the LED, as we’re going to write data to it.
  • INPUT for the push button, as we’re going to read data from it.

Now, the digital pins are correctly set up.

Loop – Turn on the LED when button is pressed

void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) {
digitalWrite(LED_PIN, HIGH);
}
else {
digitalWrite(LED_PIN, LOW);
}
}

In the loop function, we start by reading the button’s state with the digitalRead() function. As we have a pull down resistor on the button, we know that the non-pressed state will give us the value LOW.

(Note: if you were using a pull up resistor, or no resistor at all – with the INPUT_PULLUP  option for pinMode – this would be the opposite. HIGH when the button is not pressed, and LOW when it’s pressed.)

So, once we get the button’s state, we check if it’s HIGH or LOW:

  • HIGH (pressed): we power on the LED with digitalWrite() and the HIGH state.
  • LOW (not pressed): we power off the LED with digitalWrite() and the LOW state.

OK, now let’s do something a little bit more complex.

Toggle LED’s state with the push button – first iteration

What we want to do is to toggle the LED’s state when you press + release the button. So, the first time you release the button, the LED will turn on. The second time, it will turn off. Etc.

The code

#define LED_PIN 8
#define BUTTON_PIN 7
 
byte lastButtonState = LOW;
byte ledState = LOW;
 
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
 
void loop() {
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
if (buttonState == LOW) {
ledState = (ledState == HIGH) ? LOW: HIGH;
digitalWrite(LED_PIN, ledState);
}
}
}

Let’s analyze the code.

Setup

#define LED_PIN 8
#define BUTTON_PIN 7

Those defines are the same as before.

byte lastButtonState = LOW;
byte ledState = LOW;

We need to create 2 global variables, to keep the state of the button and the LED. In the loop we’ll use those variables to compare the previous state to the new state.

void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}

The void setup stays the same. We just set the mode for both pins.

And now the serious stuff is coming. We enter the void loop function.

Monitor the button’s state

void loop() {
byte buttonState = digitalRead(BUTTON_PIN);

The first thing we do is to read the button’s state and to store it inside a new local variable.

if (buttonState != lastButtonState) {

Then we can compare the current state to the last one (from the previous execution of the loop function). 4 possibilities here:

  1. LOW -> LOW (last -> current).
  2. LOW -> HIGH.
  3. HIGH -> LOW.
  4. HIGH -> HIGH.

With the condition, we only enter the next block of code if the current and last state are different. If the 2 states are the same, then we don’t enter the if and the loop function is finished for this turn.

lastButtonState = buttonState;

Also, now that we have the information we want, we directly store the current state into the last state variable, so that we have the correct value for the next time we enter the loop.

if (buttonState == LOW) {

At this point we have only 2 possibilities left:

  • Either the previous state was LOW and the current state is HIGH (not pressed to pressed).
  • Or the previous state was HIGH and the current state is LOW (pressed to not pressed).

We are only interested in the second option, so here we just have to check if the current button’s state is LOW. If that’s the case, we can now turn the LED on or off.

Toggle the LED when the button has been released

ledState = (ledState == HIGH) ? LOW : HIGH;

Here we toggle the state of the LED. I’m not a big fan of one-liners but this one is really handful when you just need to toggle a state. This will save you 3-4 lines of code for something really trivial.

Here’s the template for this one-liner: (condition to evaluate) ? if true use this value : if false use this value.

So, if the LED is currently powered on (ledState == HIGH), we assign the value LOW. If the LED is powered off (ledState != HIGH), we assign the value HIGH.

digitalWrite(LED_PIN, ledState);
}
}
}

And the last thing we need to do is of course to physically set the state for the LED, with the new computed state.

Turn LED on and off with button – using debounce

One thing you might notice with the previous experiment: sometimes when you press and release the button, the LED’s state doesn’t change, or blinks quickly multiple times.

This is because the button is physically bouncing when you press it. Thus, many false positives will be interpreted by the Arduino. What you can do to prevent that is to add a debounce delay in your code. For example, you can decide that when the program detects a change in the button’s state, it will wait 50 milliseconds before considering another change viable.

Let’s add a few lines to our previous code.

The improved code

#define LED_PIN 8
#define BUTTON_PIN 7
 
byte lastButtonState = LOW;
byte ledState = LOW;
 
unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;
 
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
 
void loop() {
if (millis() - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = millis();
lastButtonState = buttonState;
if (buttonState == LOW) {
ledState = (ledState == HIGH) ? LOW: HIGH;
digitalWrite(LED_PIN, ledState);
}
}
}
}

Now, if you run this program, you won’t have any problem with the LED’s state being changed quite randomly when you press/release the button.

And here is how we achieve that.

Debounce explained

unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;

We create 2 new global variables:

  • One to decide how long we want to wait before validating a second change in the button’s state. Here it means that if the state changes, any changes after that in the next 50 milliseconds will be ignored. 50 millis is a good value, you can experiment with lower and upper values to check the behavior.
  • Another one to keep the last time the state was changed, so we have a starting point to compare to.
void loop() {
if (millis() - lastTimeButtonStateChanged > debounceDuration) {

Then, in the loop, we only start the button/LED functionality if enough time has passed since the last time the button’s state was changed.

To do that it’s quite simple: we compare the duration since the last update time to the required debounce duration.

if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = millis();

Once the debounce delay has been passed, then we can do what we did before. And don’t forget to update the starting point for the “debounce timer” just after you detect a change in the button’s state. So, the next time the program enters the loop, it will wait for 50 milliseconds (or the value you’ve chosen) to detect new changes in the button’s state.

Conclusion – Arduino turn Led ON and OFF with button

In this tutorial you have seen how to build an Arduino circuit with an LED and a push button, and also how to control this circuit to turn the LED on and off with the button.

Even if it’s a rather simple application, there are many ways to program it. You can power on the LED only when the button is pressed, or make the LED blink when you release the button, and so on.

Another way to write the code (for the exact same functionalities) is to use Arduino interrupts – available for the boards having interrupt pins.

To go further from here, check out:

Arduino – Turn LED ON and OFF With Button的更多相关文章

  1. 使用Arduino和LED光柱显示器件轻松制作电池电压指示器

    电池有一定的电压限制,如果电压在充电或放电时超出规定的限制,电池的使用寿命就会受到影响或降低.每当我们使用电池供电的项目,有时我们需要检查电池电压电量,确定是否需要充电或更换.本电路将帮助您监测电池电 ...

  2. Arduino控制LED灯(开关控制)

    问题:当使用"digitalRead(BUT) == 1"控制LED灯时会出现"digitalWrite(LED, ledState);"的值出现跳动. 原因: ...

  3. Arduino uno LED灯实验

    http://jingyan.baidu.com/article/a65957f4e358d924e67f9bad.html

  4. Arduino关于旋转编码器程序的介绍(Reading Rotary Encoders)--by Markdown

    介绍 旋转或编码器是一个角度測量装置. 他用作精确測量电机的旋转角度或者用来控制控制轮子(能够无限旋转,而电位器只能旋转到特定位置).其中有一些还安装了一个能够在轴上按的button,就像音乐播放器的 ...

  5. Arduino 串行外设接口——W3Cschool

    来源:https://www.w3cschool.cn/arduino/arduino_serial_peripheral_interface.html Arduino 串行外设接口 由 drbear ...

  6. FL2440驱动添加(4)LED 驱动添加

    硬件信息:FL2440板子,s3c2440CPU带四个LED,分别在链接GPB5,GPB6,GPB8,GPB10 内核版本:linux-3.8.0 led驱动代码如下: 值得注意地方地方: 1,定时器 ...

  7. fl2440 platform总线led字符设备驱动

    首先需要知道的是,设备跟驱动是分开的.设备通过struct device来定义,也可以自己将结构体封装到自己定义的device结构体中: 例如:struct platform_device: 在inc ...

  8. 如何用 JavaScript 控制 Arduino?

    Arduino 运行 C 语言,而主控端运行 JavaScript,一次要编写和维护两种程序.既然浏览器和服务器都用 JavaScript,若 Arduino 也能用 JavaScript 控制,那岂 ...

  9. Arduino系列之智能家居蓝牙语音遥控灯(四)

    用到的材料 Arduino uno hc-05   蓝牙模块 安卓手机 安卓APP AMR—voice 通过安卓手机连接Arduino的蓝牙模块Hc-05,通过语音识别软件AMR-voice识别语音, ...

  10. 「雕爷学编程」Arduino动手做(20)—水银开关模块

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...

随机推荐

  1. 归并排序Java版(图文并茂思路分析)

    归并排序 工作原理: 工作原理是将一个大问题分解成小问题,再将小问题分解成更小的.(乍一看就觉得是像一个递归)就像下图这样.然后不断的将其一份为二,分解成更小的排序. 我们设一个函数叫MergeSor ...

  2. 前端vue uni-app基于uQRCode封装简单快速实用全端二维码生成插件

    快速实现基于uQRCode封装简单快速实用全端二维码生成插件; 下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=12677 效果图 ...

  3. 屏蔽CSDN百度广告

    最近在查询一些技术问题访问到CSDN时一直弹一些令人作恶的广告,说个特别的广告,脱发广告,特别有针对性程序员同胞们的共性问题,不过还是特别恶心,百度了一下,大家也特别反感,CSDN你真这么缺钱?废话不 ...

  4. 吴恩达优化算法 (Optimization algorithms)笔记

    Mini-batch 梯度下降(Mini-batch gradient descent) 使用batch梯度下降法,一次遍历训练集只能让你做一个梯度下降,使用mini-batch梯度下降法,一次遍历训 ...

  5. 2023-07-08:RabbitMQ如何做到消息不丢失?

    2023-07-08:RabbitMQ如何做到消息不丢失? 答案2023-07-08: 1.持久化 发送消息时设置delivery_mode属性为2,使消息被持久化保存到磁盘,即使RabbitMQ服务 ...

  6. Mediabox:年度最佳音视频开发工具

    "2023稀土开发者大会"落下帷幕,由稀土掘金社区评选的的掘金技术引力榜重磅出炉,共有22个优秀实践案例上榜,涵盖对技术行业发展有特别贡献的人物.开发工具.开源项目.技术团队和技术 ...

  7. Redis核心技术与实践 03 | 高性能IO模型:为什么单线程Redis能那么快?

    原文地址:https://time.geekbang.org/column/article/268262 个人博客地址:http://njpkhuan.cn/archives/redis-he-xin ...

  8. Centos7安装Python3.x

    一.修改yum源 查看Centos发行版本 cat /etc/redhat-release 换阿里云yum源 备份原始yum源 mv /etc/yum.repos.d/CentOS-Base.repo ...

  9. ABP VNext添加全局认证(如何继承AuthorizeFilter)

    前言 目前公司采用的开发框架是ABP VNext微服务框架 最近突然发现一个问题,ABP中如果控制器或服务层没有加 Authorize特性的话,则不会走身份认证,且不会认证Token 如图: 但是项目 ...

  10. 【心得】C51单片机_中断

    @ 目录 ①学习单片机中断总思想 ②学习单片机中断总思想 ③学习单片机中断总方法 外部中断 定时计数器中断 串行口中断 ④总结 附 ①学习单片机中断总思想 标题客观的说,学习单片机只需要掌握 I/O ...