Make: Projects

Build a Hot/Cold Detector with LEDs

With an Arduino, ultrasonic distance sensor, and some LEDs, we are going to build a classic "hot/cold" project.

Build a Hot/Cold Detector with LEDs

In this project, we will combine an Arduino, a Ping sensor, and a small assortment of components, to build a project that senses distances as “hot/cold.” Once built, we’ll walk through the software running our basic “sketch,” (what an Arduino program is called) and then experiment with variations of the “hot/cold” theme, all the while using the same circuit.

For the Arduino sketch files provided, the V1 sketch is detailed below. It measures distance from the sensor. The farther you are from the sensor, the “cold” blue LED begins to glow, and then the closer you get to the sensor, the “cold” LED fades away and the “hot” red LED turns up to full brightness!

The V2 sketch is a “capture the ping” game. At first, the “cold” blue LED glows, and every so often, the “hot” red LED will flash. When the red LED is on, try to move your hand in front of the sensor quickly. If you are fast enough, the red LED will flash; if you are too slow (or cheat!), the blue LED will flash.

And finally, the V3 sketch is a simple “hot/cold” switch. When no object is present in front of the sensor, the “cold” blue LED will produce a slow pulse. When it does sense an object, say when you sit down in front of your computer, the “cold” blue LED will turn off and the “hot” red one will shine at full brightness. This switch can be used to trigger other effects, such as waking your computer up from sleep mode.



Step #1: Gather all your components.

Build a Hot/Cold Detector with LEDsBuild a Hot/Cold Detector with LEDsBuild a Hot/Cold Detector with LEDsBuild a Hot/Cold Detector with LEDs
  • Believe it or not, these are all the parts you will require for this build!
  • NOTE: Resistors listed in subsequent steps are for the LEDs suggested in the parts list. If you want to use different LEDs, you'll have to calculate the resistor required using Ohm's Law. Search online for "LED calculator" to determine the resistor needed. The 500-pack of resistors is recommended so you always have a range of options available.
  • First, connect the breadboard to the Arduino. Using two jumper wires: connect one wire from the 5V pin on the Arduino to the power rail on the breadboard. Have the other go from the GND pin to the ground rail.
  • Take a look at the Ping sensor. You‘ll notice three pins next to each other, labeled GND, 5V, and Sig (as in "Signal"). Now, let's wire up the connections necessary to supply power, ground, and signal to the Ping Sensor.
  • Decide where you want your Ping sensor located on the breadboard. Make a connection between the ground rail and the sensor's GND pin row. Add another wire between the power rail and the sensor's 5V pin row. Alternatively, you could use short pieces of 20 AWG hookup wire.
  • Lastly, make a connection between the Ping sensor's Sig(nal) pin row, and the Arduino pin labeled number 7. This connection will work in two directions: It will be used to send the "ping“ from the sensor, and also transmit the signal coming back from an object in front of the sensor.

Step #2: Place the Ping sensor.

Build a Hot/Cold Detector with LEDsBuild a Hot/Cold Detector with LEDsBuild a Hot/Cold Detector with LEDs
  • Place the Ping sensor on the board so that the connections line up with the wires you just placed. Carefully check to ensure all the connections are correct. Trace the connection from the Arduino GND pin to the ground rail, to the GND pin on the Ping sensor. Do the same for the supply voltage.
  • Next, use two jumper wires which will eventually connect the LEDs to the Arduino. Place a wire from the Arduino pin 6 to the row where you want to place your blue LED. Do the same for the row intended for the red LED, and connect it to the Arduino pin 5.
  • If you‘re curious about why I left two rows empty in-between the two jumper wires on the breadboard, that's because we need a resistor for each LED (see next step for details). The resistor values are calculated by using the rated voltage and current of the LED, and the voltage supplied, and Ohm's Law.

Step #3: Connect the LEDs and resistors.

Build a Hot/Cold Detector with LEDsBuild a Hot/Cold Detector with LEDsBuild a Hot/Cold Detector with LEDsBuild a Hot/Cold Detector with LEDs
  • First, take the 150Ω resistor (brown-green-brown) and make a connection between the ground rail and the row next to the row connected to pin 5 (driving the red LED). Using the 56Ω resistor (green-blue-black), do the same for the blue LED, connecting the ground rail and the row next to the one connected to pin 6 on the Arduino.
  • All we need to do now is connect our LEDs to the board. Since LEDs, like all diodes, work only in one direction, you have to make sure to place them so the shorter leg, called cathode, is connected to GND through the resistor. The "positive“ side, called anode, has the longer leg and will be connected to the Arduino via a jumper wire.
  • Place the red LED so the shorter leg will be connected to the resistor, and the longer leg goes in the row which is connected to pin 5. Do the same for the blue LED, connecting its cathode to the resistor, and the anode to the row going to pin 6 of your Arduino.
  • A simple visual check now will save time in the future. Trace the connections to make sure everything is in its designated place. Once you're ready to load the software sketches, continue to the next step.

Step #4: Watch them glow!

Build a Hot/Cold Detector with LEDsBuild a Hot/Cold Detector with LEDsBuild a Hot/Cold Detector with LEDs
  • When everything looks okay, we‘re done building the hardware part of our project. Now, fire up your computer. If this is your first Arduino build, download the Arduino Software from
  • A lot of makers use the blink tutorial for their first build. It's a great primer for understanding the Arduino SDK, or software development kit:
  • Start the Arduino Software, and download the Hot/Cold sketch files for this project. Begin with the V1 sketch, which is a "hot/cold" proximity sketch. Hit the Upload button in the Arduino software and the sketch will compile.
  • Use your hand or an object to quickly test the Ping sensor and see how it works. Now that the hardware of your Hot/Cold LEDs is working, we will take a look at the code in the subsequent steps.
  • The V2 & V3 sketches are additional programs that use the same Arduino and breadboard hardware configuration to produce variations of the "hot/cold" theme. Once you understand the V1 sketch, upload the V2 & V3 sketches and experiment with them. Then, build your own hot/cold project!

Step #5: Calculate the ''ping'' time.

Build a Hot/Cold Detector with LEDs
  • The first lines of code are a basic configuration: declare the pins used for the sensor and the LEDs, measure the "distance" which will be used to store the distance (in cm), and determine the "pulseDuration" which stores the time it takes between sending the ping and receiving it again by the sensor.
  • In the "loop()" - the main program being run by the Arduino - you can see the three steps used to measure the distance of an object from the sensor. First, it sets the SensorPin to output and emits a 5 microsecond long impulse, the ping. Then, the SensorPin gets switched to input, and the program counts the time for the ping to return.
  • The "pulseDuration" first gets divided by two, because we measure the time the ping takes going from and back to the sensor. Then it gets divided by 29. Why 29? Our measurement is in microseconds, and sound travels at 1cm per 29 microseconds.

Step #6: The Red LED code.

Build a Hot/Cold Detector with LEDs
  • Given the distance from an object, the program calculates output for the LEDs. First, let's take a look at the code driving the red LED.
  • The Parallax Ping Sensor can measure distances up to 300cm, but we will be restricting the device (in software) to measure between 0-50cm.
  • The red LED will light up starting at 25cm, and will increase to full brightness at 0cm. First, we check if the object is within 25cm. If true, we need to translate the distance of 25-0cm into an integer value between 0 and 255, which determines the brightness of the LED.
  • Don't let the similar numbers confuse you. The value 255 is considered "always on" to the Arduino, whereas 0 is considered "off." The measurement and brightness intensity operate so that ≥25cm=0 and 0cm=255.

Step #7: The Blue LED code.

Build a Hot/Cold Detector with LEDs
  • For the blue LED, it's a bit more complex. Since it should light up between 50-25cm, but fade out between 25-10cm, we need to add an additional "if" statement to the code. Again, there is calculation to translate the distance of 10-25cm into an integer value between 0 and 255.
  • Then we do another translation for the case of distance being between 25 and 50cm. This time, an input between 25 and 50 gets translated into an integer between 255 and 0, respectively.
  • If the Distance is not within the two specified ranges in the "if" and "else if" statements above, the blue LED is "0," or "off".

Step #8: Experiment with Hot/Cold

Build a Hot/Cold Detector with LEDs
  • The last instruction is a "delay()" of 20 milliseconds, which gives us approximately 50 loops per second. This could be called the polling interval, or refresh rate, for our code.
  • Since it takes some time to execute the instructions in our loop, the actual rate is slightly lower, but there are still plenty of updates per second to trick the human eye.
  • The V2 and V3 sketches provide other examples of how to turn this exact same circuit configuration into another project, including a "capture the ping" game and a "hot/cold" on/off switch. Just download the sketches, open them in the Arduino Software, and upload them! (Comments in the sketches tell you exactly what they do.)
  • Now that you know all about the hardware and software for this build, you should hack, modify, and improve Hot/Cold LEDs by turning it into your own project!

  • Nick Normal

    Hi Imnot, you can find the version 3 files, which trigger pin 12 to wake up the PC, here:

    Good luck, and let us know if you build this project!

    • miguel

      where it connects pin 12 to wake up the PC

  • There is also a boom that can be moved up and out of the way when one wants to simply use the
    headphones. Recently many players encounter G27 problem in Shift 2: Unleashed.
    t recognize then check the USB port by connecting another device.

  • Mumble

    I have a question about this. In this guide the ping sensor is programmed to only 30 cm. how can I modify the programming to increase the sensors range?

  • The link for the variation sketches titled “download the Hot/Cold sketch files for this project.” in the text is no longer a link, I’m having difficulty with the game variation of the sketch and it would be useful to see a working version.

  • Colin Lamb

    Where do you download the program files to run the hot cold sensor?

  • Aryan King

    Parts for this project are difficult to find in india!!!!! What to do help Where I can find parts

  • gabi

    u could use this for communicating in morse code with one color dashes and another dots and put your hand in front to change color

  • Leslie

    I tried this out this weekend using this much cheaper proximity sensor ($5 instead of $30):

    The code needs to be modified to account for separate “ping” and “listen” pins.

    I also had to cut the sensing distance in the v3 code in half, because it was sensing the wall 8 feet away and the light was a constant red.

    • wait…so this ping sensor works as the parallex sensor? whats the difference in the two?

  • Chadun

    Does any other model other than parallax ping sensor works on this?

    • Leslie

      Hi Chadun – as I noted in my comment above, the SainSmart sensor worked with just an extra connection and slight modifications to the code, which I can explain in more detail if you need it. I couldn’t find another sensor that was exactly equivalent to the Parallax Ping sensor.

  • pls can u give the program codings

  • Peter

    I think the comments in the code can be a little misleading on how this sensor actually works — I can’t make sense of it as described. The code is:

    // measure distance: listen for “Ping”
     pinMode(SensorPin, INPUT);
     pulseDuration=pulseIn(SensorPin, HIGH);

    I don’t think the user code above is actually measuring distance, nor is it listening for the ping echo. Instead the *sensor* listens for the echo, processes the (probably very fuzzy) signal, and returns a pulse the length of which *represents* the distance. If you were actually listening (like a microphone) and measuring the delay, you wouldn’t do it with pulseIn HIGH. I think the sensor is designed so that pulse duration it calculates *represents* the distance, it is not a direct physical measurement.

  • Henry Campbell

    The ping sensor doesn’t light up I think I have the jumper wires in the wrong place can someone help?

    • Peter

      The LED on mine didn’t light up until I loaded the sketch. I have no idea why.

  • Pingback: Makers Making #WeekendProjects | MAKE()

  • miguel

    HOT COLD LED V3 doesnt work
    any solution?

  • Chris Valentine

    Where are the library files????

  • sorry the files got lost somehow. download them here:

  • Pingback: And the Winner is… | Building & Hacking @ York College (CT 137)()

  • Jaden Davis

    how do I download the file without typing it of of the picture?

  • Pingback: Inspirations and Ideas | 7 weeks project Wearable technology()

  • Pingback: Spy Cipher Screening (° o°)! | Building & Hacking @ York College (CT 137)()

  • Ivan

    Am I missing something? I’ve tried the code made to turn on my computer, the leds do work although it’s not waking up my computer.

    This is what I have:

    // make 3: turn on computer when in front of sensor

    int usbChipPin=12;

    int SensorPin=7;

    int BlueLedPin=6;

    int RedLedPin=5;

    int counter=0;

    int resetCounter=0;

    int ledState=1;

    int ledDirection=1;

    boolean isSend=false;

    unsigned long pulseDuration=0;

    void setup() {

    // make led pin ready

    pinMode(BlueLedPin, OUTPUT);

    // make wake up pin ready

    pinMode(12, INPUT);


    void loop() {

    // make led light steady while sensing, or dim led while idle

    if (counter>0 && isSend == false) {

    analogWrite(BlueLedPin, 200);

    } else if (isSend == false) {

    if (ledDirection == 1) {

    analogWrite(BlueLedPin, ledState);

    if (ledState == 125) { ledDirection=-1; } else { ledState = ledState + 1; }

    } else {

    analogWrite(BlueLedPin, ledState);

    if (ledState < 1) { ledDirection=1; } else { ledState = ledState – 1; }


    } else {

    analogWrite(RedLedPin, 255);

    analogWrite(BlueLedPin, 0);


    // measure distance: send "Ping"

    pinMode(SensorPin, OUTPUT);

    digitalWrite(SensorPin, HIGH);


    digitalWrite(SensorPin, LOW);

    // measure distance: listen for "Ping"

    pinMode(SensorPin, INPUT);

    pulseDuration=pulseIn(SensorPin, HIGH);

    // if someone is in reach, count up, or reset if not

    if ( pulseDuration 50) { resetCounter=0; }

    } else {

    resetCounter = resetCounter +1;

    if (resetCounter > 100) {




    analogWrite(RedLedPin, 0);



    // if someone is there for 300 loops, start up pc

    if (isSend == false && counter > 100) {

    // reset usb-to-serial chip

    pinMode(usbChipPin, OUTPUT);

    digitalWrite(usbChipPin, LOW);


    pinMode(12, INPUT);

    // don’t do it again





  • Alex Contreras

    wheres the code?

  • Pingback: Sensing Hot and Cold with Weekend Projects - Make: | Make:()

  • Pingback: Perfect Projects with Ultrasonic Range Finder for Arduino Beginners – BLOG()