In this short tutorial I show how to use a distance sensor as a switch to turn an led on and off.

View Comments (0)

In the last example I controlled the brightness of an LED with a distance sensor. This project will edit that setup so that the distance sensor works as a switch. When you hold your hand in front of the sensor for a moment, the led turns on. When you move your hand away, it turns off.

Starting with the last Embrio project, I delete the transform and add an Above or Below node. I connect the lagged value to the above or below activation value, and the Is Above output to the output node. I move my hand in front of the sensor and adjust the threshold value so that the node turns on when it is blocked. If the threshold is too low, it won’t pick up my hand until it gets close enough. I can also adjust the lag value until I like how the control reacts.

Now I can turn the LED on and off by moving my hand in front of it and moving it away. But I want the sensor to act as a switch which triggers when I move my hand in front of it, and stays on until I move my hand in front of it again. I disconnect the Above or Below node from the output, and add a switch between them. I connect the switch activation to the output node, and the Go Above trigger to the Switch’s Toggle input. Now I can turn the LED on and off my blocking the sensor for a moment.

  • A - Read the distance sensor value with an analog input pin.

  • B - Slow down the sensor value with a Value Lag node. This lets you control how long the sensor needs to be blocked before it goes above the Above or Below threshold and fires a trigger.

  • C - When the slowed down sensor value goes above the threshold, the Go Above output trigger fires.

  • D - A Switch is toggled to turn the LED on and off.

  • E - Control the LED with an Analog Output node.

View Comments (0)