Arduino 5 Minute Tutorials: Lesson 4 - IR Distance Sensor & Push Button

Posted on 17/09/2018 by cbenson in Arduino 5 Minute Tutorials
Modified on: 23/03/2023
Steps completed / 3
Press to mark a step as
completed or click here to complete all
Introduction

Welcome to RobotShop's 5 Minute Tutorials. This specific series focuses on getting started with Arduino and covers core concepts like basic code structure, interfacing with sensors, actuators, and more. This lesson covers two topics: simple push buttons which are used extensively in robotics, and infrared distance sensors. Once complete, use the table of contents to move on to the next lesson. 


  1. Computer / Laptop or Netbook
  2. Arduino Microcontroller
  3. USB to Serial Adapter (if your microcontroller does not have a USB port)
  4. Appropriate USB cable (Arduino boards draw power from the USB port – no batteries yet)
  5. IR Distance sensor (preferably Sharp) and corresponding cable
  6. Push button and corresponding cables to connect to Arduino

 

Infrared distance sensors are useful for measuring distances without actually touching a surface. The three wires protruding from a distance sensor represent +5V (in most cases), GND (Ground), and signal. These are almost always color coded with black as ground, red as +V, and white or yellow as the signal. If your infrared distance sensor did not come with any wires, you will either need to find the appropriate connector, or solder wires directly to the leads (ensure the pins and solder do not contact one another) so you can attach wires.

  1. Connect the red wire to +5V on the Arduino
  2. Connect the black wire to GND on the Arduino
  3. Connect the yellow wire to an analog pin on the Arduino (in this case we chose A2)

Since the sensor is connected to the analog input of the Arduino, the code is identical to that of the potentiometer:

Arduino 5 Minute TutorialsArduino 5 Minute Tutorials

Upload this program to the board and change it to the Serial Monitor. As you move the front of the distance sensor closer to and away from a solid object or wall, the values should change between 0 to 1023. You can now read values and use them within your code. Check the range for your sensor (not all sensors can read from zero cm); note that some sensors have a minimum distance - although it is always listed in the specifications, try to find it by experimentation. To convert the values to actual distances (in cm or inches), consult the user guide of the sensor.

Connecting toggle switches, push buttons, and momentary contact switches to the Arduino is straightforward. A push button is a simple device that completes a circuit. One end of the button is connected to the source, usually, a low voltage (5V on the Arduino is ideal), and the other is connected to the digital pin. When the switch is flipped, pressed, or toggled, the circuit is either opened or closed. The digital pin simply returns if there is 5V or 0V. The code associated with this is:

digitalRead(pin);

In the following simple program, a push button is used to turn on the LED connected to pin 13. The line

digitalWrite(ledPin, status);

turns the ledPin (in this case assigned to digital pin 13) HIGH (1) or LOW (0) depending on the status variable. We initially set the status to low (0).

Arduino 5 Minute Tutorials    

LikedLike this to see more

Spread the word

Flag this post

Thanks for helping to keep our community civil!


Notify staff privately
It's Spam
This post is an advertisement, or vandalism. It is not useful or relevant to the current topic.

You flagged this as spam. Undo flag.Flag Post