# How to use a certain distance to turn on an led strip using a Photon?

Hi!

I am working with the Particle Photon and an IR Sharp distance sensor (which I both have never used before, so please take it easy on me). I’ve written a simple code in the Particle Build to test the distance sensor. When your hand is close to the sensor, the led strip turns blue. If you remove your hand, the led strip goes out. It works fine, but I want to be able to use a certain distance. Right now, it just works with ‘if (sensorState == HIGH’. The led strip turns blue when I put my hand between like 10 to 20 cm away from the sensor, but I want that space to be a little bigger, like 30 to 50 cm. I’ve tried an example that uses ‘if (distance >= 80)’, but I don’t quite understand the rest of the code ( http://forum.arduino.cc/index.php?topic=67766.0 ).

This is my own code:

``````const int sensorPin = A1;          //(IR distance sensor)
const int ledPin = D0;                //(Blue from rgb led strip)

int sensorState = 0;

void setup( ) {
pinMode(sensorPin, INPUT);
pinMode(ledPin, OUTPUT);
}

void loop( ) {
if (sensorState == HIGH) {
analogWrite(ledPin,255);
}   else {
analogWrite(ledPin,0);
}
}
``````

So, does anybody know how I can tell my led strip to turn blue, only when my hand is 30-40 cm away from the sensor?

@marissavw, which Sharp IR sensor model are you using?

You want to use `analogRead()` but you also need to make sure your sensor doesn’t give you more than 3.3V on the pin (hence stating the actual sensor type would be required).

At the link you provided you’ll find this

``````void loop() {
...
float volts = analogRead(IRpin)*0.0048828125;   // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 27*pow(volts, -1.10);          // worked out from graph 65 = theoretical distance / (1/Volts)S
...
}
``````

And for the Photon you’d need to change the constants

``````  float volts = analogRead(IRpin) * 3.3 / 4095.0;
float distance = 27*pow(volts, -1.10);          // worked out from graph 65 = theoretical distance / (1/Volts)S
``````
2 Likes

@ScruffR, totally right about the voltage! I believe there may also be library for these sensors and thus the request.

2 Likes

Agreed!
I also like using ready made libraries, but I also like people to understand what they are dealing with (before refering to “black box” code ;-))

2 Likes

I am using the GP2Y0A21YK sensor.

Right now I’m using the Vin pin for the sensor, should I change this to the 3v3 pin?

I’ve tried to include those two lines into my code this afternoon, but I couldn’t make it work. (Haven’t tried it with ‘3.3 / 4095.0’ yet.) I got an error saying ‘pow was not declared in this scope’. I couldn’t figure it out by searching online, because I’m so new to this and I’m Dutch. Can you tell me what that error means?

I’ll work on it again tomorrow. Thanks!

In order to use `pow()` you need to add `#include <math.h>` - in that header the missing function will be declared and henceforth be known (visible) in your code (scope).

You can power it off 3v3, but the datasheet states a max output of 3.1V @ 10cm even when powered higher, so Vin should be OK too - I’d still go with 3v3.

Oh okay, I didn’t know that. I will give it another try tomorrow! Thanks a lot.