🔬
Wiki.Linhkientot
  • 👋Chào mừng tới Linhkientot's Wiki
  • Hướng dẫn phần mềm
    • Cài đặt Arduino IDE 2.0
    • Cài đặt phần mềm Mixly
    • Cài đặt phần mềm Mind+
    • Cài đặt phần mềm Thonny
  • Arduino Products
    • Hướng dẫn ban đầu Arduino IDE
      • Hướng dẫn nạp chương trình Arduino IDE
      • Thêm thư viện Arduino IDE
      • Cài đặt Driver CH340 trên MAC OS
      • Lập trình Board ESP32 trên Arduino IDE
    • Arduino Starter Kit
      • Lesson 0. Getting Started - install IDE
      • Lesson 2. LEDs
      • Lesson 3. RGB LEDs
      • Lesson 4. Eight LEDs and a Shift Register
      • Lesson 5. The Serial Monitor
      • Lesson 6. Digital Inputs
      • Lesson 7. Make an RGB LED Fader
      • Lesson 8. Analog Inputs
      • Lesson 9. Sensing Light
      • Lesson 10 Making Sounds
      • Lesson 11 LCD Displays Part 1
      • Lesson 12 LCD Displays Part 2
      • Lesson 13 DC Motors
      • Lesson 14 Servo Motors
      • Lesson 15 DC Motor Reversing
      • Lesson 16 Stepper Motors
      • Lesson 17 Email Sending Movement Detector
    • Arm Robot
      • Hướng dẫn lắp ráp Arm-4DoF
      • Hướng dẫn lắp ráp Arm-6DoF
      • Hướng dẫn lắp ráp Arm - 6DoF đế tròn quay
    • Car Robot
      • Hướng dẫn lắp ráp tank TS-100
    • Arm Car Robot
    • Smart home IoT
    • Lập trình cảm biến khí ga/ khói MQ-2 Arduino
  • Micro:bit Products
    • Micro:bit Setup
    • Micro:bit Starter Kit V1
      • Setting up the micro:bit with Makecode
      • Set up Arduino IDE for micro:bit
      • Using the Buttons and LED Matrix on micro:bit
      • Pushbutton with micro:bit
      • Tilt Sensor with micro:bit
      • Temperature Sensor with micro:bit
      • DC Motor with micro:bit
      • Sound Sensor with micro:bit
      • Raindrop Sensor with micro:bit
      • Make an RGB LED Blink with micro:bit
      • Ultrasonic Distance Sensor with micro:bit
      • Analog Inputs and micro:bit
      • Servo with micro:bit
      • Smoke sensor with micro:bit
      • Light-dependent Resistor with micro:bit
      • Infrared Obstacle Avoidance Sensor with micro:bit
      • Using LEDs with micro:bit
    • Micro:bit Starter Kit V2
      • Microbit Introduction
      • Microbit Basic Lessons
      • 1. Lesson: Đèn LED nhấp nháy
      • 2. Lesson: RGB Led
      • 3. Lesson: Đọc giá trị từ triết áp
      • 4. Lesson: Đọc nút nhấn
      • 5. Lesson: Servo Motor
      • 6. Lesson: Passive Buzzer-Còi thụ động
      • 7. Lesson: Active Buzzer-Còi chủ động
      • 8. Lesson: Compass-La bàn
      • 9. Lesson: Accelerometer-Gia tốc kế
      • 10. Lesson: Module phát hiện âm thanh
      • 11. Lesson: Cảm biến ánh sáng (quang trở)
      • 12. Lesson: Cảm biến ngọn lửa
      • 13. Lesson: Cảm biến khói
      • 14. Lesson: Cảm biến siêu âm
      • 15. Lesson: ModuleRelay
      • 16. Lesson: Cảm biến nhiệt độ (DHT11)
      • 17. Lesson: Bộ điều khiển từ xa (IR)
      • 18. Lesson: Hiển thị màn hình LCD i2c 1602
      • 19. Lesson: Hiển thị nhiệt độ, độ ẩm trên màn hình LCD
      • 20. Lesson: Điều khiển quạt bằng Relay
    • Micro:bit Advanced Kit V2
      • Giới thiệu BBC Sensor Shield V2
      • Danh sách linh kiện bộ Kit
    • Micro:bit Car
  • ESP32 PRODUCTS
    • Hướng dẫn lập trình ESP32
  • Raspberry Products
    • Raspberry Pico Kit
    • Raspberry Pico Car
  • Group Robots
    • 🤖Arduino Robots
    • 🐦Micro:bit Robots
    • 🍓Raspberry Robots
Powered by GitBook
On this page
  • Arduino Lesson 6. Digital Inputs
  • Overview
  • Parts
  • Breadboard Layout
  • Arduino Code
  • Push Switches
  • Other Things to Do
  1. Arduino Products
  2. Arduino Starter Kit

Lesson 6. Digital Inputs

PreviousLesson 5. The Serial MonitorNextLesson 7. Make an RGB LED Fader

Last updated 1 year ago

Arduino Lesson 6. Digital Inputs

Created by Simon Monk

Last updated on 2022-12-01 01:52:30 PM EST

Table of Contents

Overview 3

Parts 3

  • Part

  • Qty

Breadboard Layout 8

Arduino Code 9

Push Switches 11

Other Things to Do 11

Overview

In this lesson, you will learn to use push buttons with digital inputs to turn an LED on and off.

Pressing the button nearer the top of the breadboard will turn the LED on, pressing the other button will turn the LED off.

Parts

To complete this lesson, you will need the following parts.

270 Ω Resistor (red, purple, brown stripes) 1

Half-size Breadboard 1

Arduino Uno R3 1

Jumper wire pack

1

Breadboard Layout

Although the bodies of the switches are square, the pins protrude from opposite sides of the switch. This means that the pins will only be far enough apart when they are the correct way around on the breadboard.

Remember that the LED has to be the correct way around with the shorter negative lead to the right.

Arduino Code

Load the following sketch onto your Arduino board. Pressing the top button will turn the LED on, pressing the bottom button will turn it off again.

/*

Adafruit Arduino - Lesson 6. Inputs

*/

int ledPin = 5;

int buttonApin = 9; int buttonBpin = 8;

byte leds = 0; void setup()

{

pinMode(ledPin, OUTPUT); pinMode(buttonApin, INPUT_PULLUP); pinMode(buttonBpin, INPUT_PULLUP);

}

void loop()

{

if (digitalRead(buttonApin) == LOW)

{

digitalWrite(ledPin, HIGH);

}

if (digitalRead(buttonBpin) == LOW)

{

digitalWrite(ledPin, LOW);

}

}

The first part of the sketch defines three variable for the three pins that are to be used. The 'ledPin' is the output pin and 'buttonApin' will refer to the switch nearer the top of the breadboard and 'buttonBpin' to the other switch.

The 'setup' function defines the ledPin as being an OUTPUT as normal, but now we have the two inputs to deal with. In this case, we use the set the pinMode to be 'INPUT_PULLUP' like this:

pinMode(buttonApin, INPUT_PULLUP); pinMode(buttonBpin, INPUT_PULLUP);

The pin mode of INPUT_PULLUP means that the pin is to be used as an input, but that if nothing else is connected to the input it should be 'pulled up' to HIGH. In other words, the default value for the input is HIGH, unless it is pulled LOW by the action of pressing the button.

This is why the switches are connected to GND. When a switch is pressed, it connects the input pin to GND, so that it is no longer HIGH.

Since the input is normally HIGH and only goes LOW, when the button is pressed, the logic is a little up-side-down. We will handle this in the 'loop' function.

void loop()

{

if (digitalRead(buttonApin) == LOW)

{

digitalWrite(ledPin, HIGH);

}

if (digitalRead(buttonBpin) == LOW)

{

digitalWrite(ledPin, LOW);

}

}

In the 'loop' function there are two 'if' statements. One for each button. Each does an 'digitalRead' on the appropriate input.

Remember that if the button is pressed, the corresponding input will be LOW, if button A is low, then a 'digitalWrite' on the ledPin turns it on.

Similarly, if button B is pressed, a LOW is written to the ledPin.

Push Switches

Switches are really simple components. When you press a button or flip a lever, they connect two contacts together so that electricity can flow through them.

The little tactile switches that are used in this lesson have four connections, which can be a little confusing.

Actually, there are only really two electrical connections, as inside the switch package pins B and C are connected together, as are A and D.

Other Things to Do

There are a couple of things we could try with this hardware.

Firstly, you could try taking what you learnt in lesson 5 and adding some commands to the sketch that print something to the Serial Monitor whenever either switch is pressed.

Remember that as well as printing out a message using something like this in your 'loop' function:

Serial.println(“Button A Pressed”);

You will also need to start serial communication in the 'setup' function by doing this:

while (!Serial); Serial.begin(9600);

A second modification that you could make would be to make the buttons do something different. So, for example you could change the sketch so that if button A is pressed, the LED turns on, but then turns off again after 30 seconds.

Hint: Think of this as being a very slow blink.

Part Qty

5mm Red LED 1

Tactile push switch 2