In this tutorial I’ll set up and use an Infrared distance sensor to control an LED.
Like most of these project I already have an LED plugged into digital pin 3. The distance sensor has 3 wires, one for voltage, one for ground, and one for signal. I plug the red and black wires into the voltage and ground channels, and the yellow signal wire into analog pin 2.
In Embrio I add a controller input and output node. My LED is plugged into pin 3, so I can leave the output node alone. On the input node I select pin A2, then connect to the Arduino. After the program uploads I start getting a reading from my sensor. If I move my hand in front of the sensor I can see the activation change. To make it easier to see what’s going on, I open up the graph for the input node. I notice 2 things about the input signal. First, it’s pretty noisy. Second, it doesn’t fit the 0 to 1 range that I want it to. To clean up the noise, I first add a value lag node from the math menu and attach the input node to it. I adjust the Lag value high enough so that the value is smooth, but not so high that it lags behind too far. A value around . 6 looks pretty good.
Next I want to adjust the range so that when nothing is blocking the sensor it outputs a 0, and when something is as close as it can be, it shows a 1. I add a transform number node from the math menu and hook up the value lag node to it. I want the output range to stay from 0 to 1, so I’ll leave those inputs alone. I’m going to adjust the input range minimum and maximum values. First, with nothing in front of the sensor, I’ll adjust the Input Min value until the output activation is 0. Next I put my hand in front of the sensor until the graph peaks. Because of the way these IR sensors work, there’s a bit of a blind spot in front of them, so the maximum value will actually be a little bit out from the sensor.
I see that the highest value I’m getting is around .6, so I adjust the max value to that. I adjust my graphs to compare the noisy input signal to the nice clean output signal I just set up. Finally I connect the transformed activation to the output node and now I can control the brightness of the led by moving my hand in front of the distance sensor.
A - Read the raw distance sensor value on analog pin A2.
B - Clean up the noisy input signal with a Value Lag node.
C - Use a Transform Value node so that the signal uses the entire range of 0 to 1.
D - Control the brightness of the LED with an analog output.