i alread have this code, but i do not know how to make the led blink.


const int ledPin = 6; // The pin of your LED, D06
const int trigPin = 5; // The pin to which the TRIG is connected, D05
const int echoPin = 4; // The pin to which the ECHO is connected, D04
const int ledOnTime = 1000;
// The time that the LED stays on, after detecting the motion (in milliseconds, 1000ms = 1s)
const int trigDistance = 40;
// The distance (and lower than it) at which the sensor is triggered (in centimeters)
int duration;
int distance;

void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);

void loop() {
    digitalWrite(trigPin, LOW);
    digitalWrite(trigPin, HIGH);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = duration * 0.034/2;
    if (distance <= trigDistance) {
        digitalWrite(ledPin, HIGH);
        digitalWrite(ledPin, LOW);
  • I corrected the formatting and also added indention. You should really use indention. It makes the code way more readable. The Arduino IDE even has an autoformatting function to introduce correct indention
    – chrisl
    Nov 12 '19 at 7:58

I would say the way to go is using non-blocking code with millis(). Look into the BlinkWithoutDelay example, that comes with the Arduino IDE. It doesn't use delay(), which is considered bad coding practice, since it is just busy waiting. Nothing else can happen during a delay, except for interrupts. Instead of waiting it is better to regularily check the clock, if it is time to do something (much like you would do in real life for example for baking a pizza).

The clock is provided by millis(), which returns the number of milliseconds since startup. Below you see the BlinkWithoutDelayexample. The variable interval controls the blink frequency. If you remove the const keyword before it and change it according to the measurements from the ultrasonic sensor, you will get a changing blinking frequency with changing distance.

Try to combine the codes into one. Remove all delay() calls, except for the delay(1) (it is needed to trigger the ultrasonic sensor and it is small enough to not matter greatly).

const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, 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) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);

The rest is up to you. If you need more information about the BlinkWithoutDelay example, you should google it. There are tons of tutorials. This is a great coding excerise, so have fun :)


step 1: define your MIN_DISTANCE, MAX_DISTANCE

step 2: get distance from sensor

step 3: rescale the distance value to a value in between 0 and 255

step 4: fade led according to the rescaled value.

Note that: you should not use delay() to blink LED. Use analogWrite() instead. See the example. We can change frequency of PWM to make LED from fading to blinking by using setPwmFrequency() function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.