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:

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

Arduino 5 Minute Tutorials

 

 

Tags:

2 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.^

Leave a Reply

  • (will not be published)