Posted on by & filed under Arduino Tutorials, Electronics, Schools & Curriculum, Software and Apps.

Lessons Menu:

Lesson 4 Hardware:

  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)


Arduino 5 Minute Tutorials

Arduino 5 Minute Tutorials

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

Arduino 5 Minute Tutorials

Upload this program to the board and change 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.

Arduino and Push Buttons

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 source, usually a low voltage (5V on the Arduino is ideal) and the other 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:


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 be low (0).

Arduino 5 Minute Tutorials




7 Responses to “Arduino 5 Minute Tutorials: Lesson 4 – IR Distance Sensor & Push Button”

  1. Cristie Demagistris

    toggle switches are very handy specially if your project requires momentary on switch.^

  2. elie

    what about if i wanna install 4 infrared sensors in X position . do you have the codes that i do have to enter it. i am thinking adapt this arduino to my ardopilot controller. motors on my drone.

    • Coleman Benson

      @elie at this time we don’t create custom code, but suggest you post your project on the RobotShop Forum and perhaps the community can provide some input.

  3. Rayan

    Can you help me to figure out this; I have one IR receiver and 5 remote controllers. I need to read all the different signals and categorize them . I can’t understand how to write the code.because each remote controller and buttons have different codes. Please help me.

    • Coleman Benson

      @Rayan Can the IR receiver detect all signals from all remotes? If so, you’ll need to understand the signals from each button from each remote and all that code to the program. The Arduino won’t really care which remote the signal is being sent from (unless of course you have it in a lookup table). This lesson deals with IR distance as opposed to IR data, so if you need more information, we suggest creating a new topic on the RobotShop Forum.

Leave a Reply

  • (will not be published)