Dimming led with IR proximity sensor

Hi everybody,

I'm trying to dim some LEDs with an IR proximity sensor (both are connected to the Arduino UNO). I would the most we're close from the sensor, the most the light is low.
I came up to switch between to power values but I don't come up to dim it. I think it's pretty simple, but I don't know how to do it. I made some research and all I found this person that tried to make almost the same thing but unfortunately the code doesn't work for me :confused:

Here's mine:

int x;
int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by



void setup() {
  //initialize serial communications at a 9600 baud rate
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop() {
  x= analogRead(0);
  Serial.println(x);


  // set the brightness of pin 9:
  analogWrite(led, brightness);
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;
  // reverse the direction of the fading at the ends of the fade:
  

  // automatically dim the led
  /*if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }*/


  // switch between two values according to the value of the sensor.
  /*if(x >= 200) {
    brightness = 255;
  } else {
    brightness = 20;
  }*/


  
  // wait for 30 milliseconds to see the dimming effect
  delay(35);
}

Thanks so much for your help :slight_smile:

Alexandre

Removing all the comments, your loop() function looks like:

void loop()
{
  x= analogRead(0);
  Serial.println(x);

  analogWrite(led, brightness);
  brightness = brightness + fadeAmount;

  delay(35);
}

Where is the brightness of the LED supposed to be related to the distance? What value IS being printed?

In the code I see things for fading a led but you don't talk about that... And you do a reading of A0, is that the proximity sensor?

If so, all you need to do is read the ADC and net the brightness

const byte LedPin = 9;           // the PWM pin the LED is attached to
const byte ProximityPin = A0;



void setup() {
  //initialize serial communications at a 9600 baud rate
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop() {
  int distance = analogRead(ProximityPin);
  Serial.println(distance);


  // set the brightness of the led:
  analogWrite(LedPin, distance / 4);
}
]/code]

And if you want to fade you have to tell us a bit more about that :)

PaulS:
Removing all the comments, your loop() function looks like:

void loop()

{
  x= analogRead(0);
  Serial.println(x);

analogWrite(led, brightness);
  brightness = brightness + fadeAmount;

delay(35);
}



Where is the brightness of the LED supposed to be related to the distance? What value IS being printed?

Hi and thanks for your reply.
Actually there's nothing about the brightness and and the distance simply because I don't know how to do it, that's why i'm here :slight_smile:

septillion:
In the code I see things for fading a led but you don't talk about that... And you do a reading of A0, is that the proximity sensor?

If so, all you need to do is read the ADC and net the brightness

const byte LedPin = 9;           // the PWM pin the LED is attached to

const byte ProximityPin = A0;

void setup() {
  //initialize serial communications at a 9600 baud rate
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop() {
  int distance = analogRead(ProximityPin);
  Serial.println(distance);

// set the brightness of the led:
  analogWrite(LedPin, distance / 4);
}
]/code]

And if you want to fade you have to tell us a bit more about that :slight_smile:

Hi, thanks for your reply too.
I forget to remove fading lines sorry about that. Yes the A0 is the proximity sensor.
i'm trying to fade indeed, so what i'm trying to do is, the most an object is close from the proximity sensor, the most the led will be faded and the most an object is far from the proximity sensor, the most the light will be shiny.

Thanks again for your reply :slight_smile:

Alright, give A0 a name and some comments :wink:

"the most" is "the more" I guess?

But "the more a led will fade" is not something. It fades or it does not. Do you mean the brighter the led? Or the faster it will fade? Or both?

But you will need to make the reading to the brightness and/or speed. What is the range of the proximity sensor? What is the desired range of brightness and/or speed?

To make fading easy, have a look at the library in my signature :slight_smile:

Thanks for your quick reply :slight_smile:

what do you mean with "give a name to A0"? Sorry i'm quite new with Arduino :confused:
Ah yes, sorry, wrong word... So, the most an object is close from the sensor, the most the brightness decrease, and the most an object is far, the most the brightness increase. I think it will be better explained like that :slight_smile:

About the range, it will be between 0 (100% brightness) and 200 (20% brightness) it's approximate, I need to make some test with the LEDs to know the exact values.

I dowload library that seems to be cool, but when I tried to use setBrightnessWithFade example, I've this error:
No error with the other examples.

Arduino: 1.6.12 (Mac OS X), Board: "Arduino/Genuino Uno"

/Users/Alexandre/Documents/Arduino/libraries/FadeLed-master/examples/setBrightnessWithFade/setBrightnessWithFade.ino:18:21: fatal error: Bounce2.h: No such file or directory
#include <Bounce2.h>
^
compilation terminated.
exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Thanks again for all,

Alexamdre

lxnr-p:
what do you mean with "give a name to A0"? Sorry i'm quite new with Arduino :confused:

Instead of using A0 in your code, make a variable with a name that tells what it is.

const byte ProximityPin = A0;

//and to use it
analogRead(ProximityPin);  //Now it's clear from the code what you do

lxnr-p:
Ah yes, sorry, wrong word... So, the most an object is close from the sensor, the most the brightness decrease, and the most an object is far, the most the brightness increase. I think it will be better explained like that :slight_smile:

After Find and Replace in that sentence as well :wink: Okay, so the brightness

lxnr-p:
About the range, it will be between 0 (100% brightness) and 200 (20% brightness) it's approximate, I need to make some test with the LEDs to know the exact values.

So when the proximity sensor gives back 0 you want 100% brightness and when it gives you 200 you want 20% brightness?

And what about it it reads more then 200? Keep it at 20%?

The PWM is a value between 0 and 255, so 20% will be a value of 51.

lxnr-p:
I dowload library that seems to be cool, but when I tried to use setBrightnessWithFade example, I've this error:
No error with the other examples.

The error alone already tells you what's wrong, you are missing the Bounce2 library. And if you would have read the text you would have known :wink: See line 7 of that example :wink:

So the code will become something like:

#include <FadeLed.h>

const byte ProximityPin = A0;

Fadeled led(9); //fading led on pin 9


void setup() {
  //Set update interval to 10ms
  FadeLed::setInterval(10);
  
  //set fade time to 6,25 seconds. So a fade between 0% and 100% will take 6,25 seconds. 
  //So between 100% and 20% will take 5 seconds (because it' only 80% of the range
  led.setTime(6250);
}

void loop() {
  //We always need to call this
  FadeLed::update();
  
  //Read the proximity and print it to serial
  int distance = analogRead(ProximityPin);
  Serial.println(distance);
  
  //cap it to 200 max
  if(distance > 200){
    distance = 200;
  }
  
  //map the distance 0 to 200 to the brightness of 100% (255) to 20% (51) 
  //and set that as the new brightness of the led.
  led.set( map(distance, 0, 200, 255, 51));
}

Thanks so much again

Oh ok, I didn't understood about A0, indeed it's better.

So when the proximity sensor gives back 0 you want 100% brightness and when it gives you 200 you want 20% brightness?

Yes exactly

And what about it it reads more then 200? Keep it at 20%?

Yes exactly too :slight_smile:

The error alone already tells you what's wrong, you are missing the Bounce2 library. And if you would have read the text you would have known :wink: See line 7 of that example :wink:

Sorry I didn't understood that "#include <Bounce2.h>" means a missing library. Indeed now it's obvious. I'm coding all the day with processing since a month and I think i'm quite saturated x) I miss some obvious things.

#include <FadeLed.h>

const byte ProximityPin = A0;

FadeLed led(9); //fading led on pin 9


void setup() {
  //Set update interval to 10ms
  FadeLed::setInterval(10);
  
  //set fade time to 6,25 seconds. So a fade between 0% and 100% will take 6,25 seconds. 
  //So between 100% and 20% will take 5 seconds (because it' only 80% of the range
  led.setTime(6250);
}

void loop() {
  //We always need to call this
  FadeLed::update();
  
  //Read the proximity and print it to serial
  int distance = analogRead(ProximityPin);
  Serial.println(distance);
  
  //cap it to 200 max
  if(distance > 200){
    distance = 200;
  }
  
  //map the distance 0 to 200 to the brightness of 100% (255) to 20% (51) 
  //and set that as the new brightness of the led.
  led.set( map(distance, 0, 200, 255, 51));
}

Thanks so much again (sorry maybe I say it too much) but it doesn't do a thing :confused: I don't understand why. Everything is plug to the right pins on the Arduino, there's some things I don't understand in the code, but the soft don't return errors, and there's no values from the proximity sensor printed in the serial monitor...

Here how I plug everything

PROXIMITY SENSOR:
yellow wire –> A0
red wire –> 5V
black wire –> GND

LED:
red wire –> 9~
black –> GND

Thanks again :slight_smile:

Alexandre

I come up to have the values of the proximity sensor print in the serial monitor by adding this: Serial.begin(9600);
But still no light :cry:

Ah, yeah, started with a clean setup(), forgot to add the start of Serial :smiley:

And damn, I found a hard case for the library! But the fix is pretty simple, don't call led.set() that often. If you call it before the led had time to fade it would reset the current fading and start with the new value

#include <FadeLed.h>

const byte ProximityPin = A0;

FadeLed led(9); //fading led on pin 9

unsigned long previousMillis;


void setup() {
  //Set update interval to 10ms
  FadeLed::setInterval(10);
 
  //set fade time to 6,25 seconds. So a fade between 0% and 100% will take 6,25 seconds.
  //So between 100% and 20% will take 5 seconds (because it' only 80% of the range
  led.setTime(6250);
}

void loop() {
  //We always need to call this
  FadeLed::update();
  
  //makes us check the distance 5 times a second
  if(millis() - previousMillis > 200){
    previousMillis = millis();
    //Read the proximity and print it to serial
    int distance = analogRead(ProximityPin);
    Serial.println(distance);
   
    //cap it to 200 max
    if(distance > 200){
      distance = 200;
    }
   
    //map the distance 0 to 200 to the brightness of 100% (255) to 20% (51)
    //and set that as the new brightness of the led.
    led.set( map(distance, 0, 200, 255, 51));
  }
}

Might try to fix this in an update of the library but it's not that easy... So it might not be worth it.

Ahah no problem ^^ And Thank you so so much !!!
It works perfectly that's really amazing.

Thanks you so much again :D, thanks to you, I may be have a weekend ^^

I say it so many time, but I don't care, Thank you1 :slight_smile: