Blinking LEDS with motion sensor

Hi All, I am very new to Arduino and electronics. I need to create a very basic and crude device which has two alternating blinking LEDS. But I want the blinking of the LEDs to speed up when someone goes near them, what do I need to do this?

please be as obvious as possible as I have no electronics background.


You could use an ultrasonic sensor like an SRF04 or a Ping. Then simply "map" the range value to a blink rate.

If you go over to the Playground, you'll see examples for ultrasonic or IR proximity sensors.

You need a proximity detector that measures distance. There are lots of them but this is popular:- It helps if you fill in your location so we can advise a source local to you. See that site for examples about how to use it. For the blinking see the blink without delay in the examples menu of your arduino IDE.

hiya, sorry I am based in London, United Kingdom

Thanks for the reply, what else will I need to get to do this project

You need to decide over what range you want to detect the person / object, and possibly over what angle. This will help decide the best type of sensor.


the proximity will be about 12 inches

Do you have any other constraints, like it has to work outdoors or in high ambient light or noise levels?

needs to work in normal room lighting, and the item will be displayed in a gallery so will be a quiet environment

sorry about not being descriptive enough, just dont know enough about it all :~ :~

sorry about not being descriptive enough, just dont know enough about it all

That's OK - just don't want you to buy a sensor that is unsuitable.

Do you need to detect when the person is approaching from the side, or only when in front of the exhibit?

thankyou for your patience :slight_smile:

I would like it to sense them approach from within 12 inches all around the LEDs, so a 12" diameter with the LEDs as the centre point

I am based in London, United Kingdom

So can you go back to your profile page and update the location accordingly. This is a global forum and sometimes it matters where you are.

within 12 inches all around the LEDs

Is this a 360 degree range, if so it is likely you will have to use more than one sensor.

Now you need to specify what sort of LEDs you are going to use? Normal 3mm or 5mm LEDs taking 20mA or more powerful ones? Normal LEDs can be driven direct from the arduino using just a resistor to limit the current. More powerful LEDs require special driving circuits called constant current circuits.

In the UK you can get that IR sensor from here:-

They also sell ultrasonic sensors I think either would suite.


I would looking for LEDs powered by the arduino,

will update my profile asap

OK great, I have modified a bit of code to help you, you only need add the distance measurement bits at the end of the loop() function where I said.

/* Blink without Delay

 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen

 This example code is in the public domain.

Modified by Grumpy Mike for TheVerse

// constants won't change. Used here to 
// set pin numbers:
const int redPin =  2;      // the number of the LED pin
const int greenPin = 3;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(greenPin, OUTPUT);    // make the pins outputs
    pinMode(redPin, OUTPUT);   

void loop()
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) { // we need to change the LEDs
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // alternate the LED that is on
         if (ledState == LOW){
        ledState = HIGH;
        digitalWrite(redPin, LOW);
        digitalWrite(greenPin, HIGH);        
        ledState = LOW;
        digitalWrite(redPin, HIGH);
        digitalWrite(greenPin, LOW);        
       } // end of led state if()
  } // end of do we need to change LEDs
  // In this part do your distance measurement
  // use the result to change the value of the variable 'interval'
  // that will change the flashing rate
} // end of loop()

Best of luck putting it together.

Hiya, thanks for this, could you recommend which parts I should get


so I have my Arduino and all the bits and pieces I need,just need the proximity sensor to come through the post. Been having loads of fun with the arduino and LEDs :-) it really is amazing fun

Anyone got any tips on how I would put it together on the breadboard

tips on how I would put it together on the breadboard

Well I would suggest that you don't. Bread board is only for a temporary setup and it causes no end of problems at that. Basically the connections are not reliable enough for a permanent installation. I would suggest you solder it all up on strip board.

First you need to draw the schematic and then place the parts so they connect up like the schematic. Don't worry too much about letting the strips carry the signals. When they can it is good, when not join with wire. When breaking strips with a drill or better spot face cutter make sure all of the copper is cut through as it is easy to leave a small whisker, especially easy when your eyes start to go.

hi Mike,

thanks for this, am I not better off trying it out on a breadboard to make sure I get it to work and then do it permanently? I am very very new to this so am likely to make a mistake

am I not better off trying it out on a breadboard to make sure I get it to work and then do it permanently

Yes that is one approach, however if your schematic is correct to start with there should be little to change. Rather in the same way as Parkinson's law states that work expands to fill the time allotted to it, electronic circuit tend to take the space of the board you allot to them.

When I left school and got my first job one of the tasks was building circuits designed by the engineers to fit into modules with a fixed size of strip board. I found that no matter how simple the circuit was the board always looked full.

There is no substitute for just going at it. Don't get bogged down by planning every step.

its just I barely know where to even start, this is the first time I have touched boards and wiring of this sort, I don't know how to draw up schematics, at the moment I have been reading loads of tutorials and trying to put things together without ruining the board

I don't know how to draw up schematics,

Well there are two parts to a schematic, the symbols and the wires that connect them. If you have modules then just draw a box and label the outputs / inputs. There is no need to have a physical relationship between the position on the box and the position on the actual module.

Keep all the grounds to the bottom of the schematic and all power towards the top. Signal flow should be from left (input) to right (output) but you can't always keep to that.

The best schematics are simple with few crossing wires.