Touchless Kitchen Lighting with Arduino and Phototransistor

Here’s a cool Arduino project I did to bring a bit of high-tech magic to my daughter’s toy kitchen. She can now turn on the lights in her toy kitchen with a simple wave of her hand!

My daughter has been excited about the thought of getting lights in her kitchen. And I was excited about the chance to play with phototransistors. So I decided to create a touchless on/off switch using an IR LED and a phototransistor that activates an LED strip.

By using an Arduino I could implement a simple algorithm to adapt to different light settings, and thereby making the phototransistor circuit super simple.

Follow along to see how you can add a touchless light switch to your own projects.

Materials and Tools

I decided to try out the new Arduino UNO R4 for this project. But any Arduino board will do the trick. Below I’ve listed the components used:

  • Arduino UNO R4 Minima
  • Infrared (IR) LED
  • Phototransistor
  • White LED Strip
  • Connector for LED strip
  • Resistor 100Ω
  • Resistor 2 MΩ
  • IRFZ24NPBF – MOSFET (nMOS)
  • Stripboard/Perfboard
  • Screw Terminals
  • 4-pin Male Pin Headers
  • 12V Power supply

Circuit Diagram and Schematic

Below is the circuit diagram and schematic for the touchless lighting system. The IR LED and phototransistor are positioned in such a way that when a hand is near the LED, it reflects infrared light back to the phototransistor.

Arduino phototransistor touchless light switch

It took some adjusting to get the phototransistor and IR led positioned in such a way that I would get a clear difference in reading between hand and no hand.

Since the LED strip needed 12V, I used a 12V wall adapter to power the Arduino. Then I could give the LED strip its 12V from the Arduino pin VIN.

Building The Circuit Board

First, I made a prototype on a breadboard. I had to experiment a bit with R2 to find a good value.

Once I had a value that worked with my phototransistor, I used Circuit Canvas to plan out how to connect the everything to a stripboard: The IR LED, phototransistor, two resistors, and nMOS transistor.

With that outline, I had a good idea of how to connect everything.

But to reduce the amount of cables needed, I modified the final circuit board a bit so that it could plug directly into my Arduino UNO as a shield. And I added screw terminals to easily connect the LED strip cables.

Circuit board place onto my Arduino

For the IR LED and phototransistor, I 3D printed a simple holder. I used some wires and pin headers to connect these to the circuit board so that I could place them independently from the board.

Finally, I connected the LED strip connector to the LED strip, then taped the LED strip to the roof of the toy kitchen. Most LED strips already have double-sided tape underneath, so did mine, so I only had to remove the protective tape then stick it.

I used a LED strip connector with screw terminals to easily be able to attach the wires from the circuit board to the LED strip.

LED-strip attached to toy kitchen
LED-strip connector with screw terminals

I positioned the IR LED and Photoresistor looking down from the top, then connected all the wires according to the wiring diagram below.

I already had the code uploaded onto the Arduino from my prototyping phase, so as soon as I connected everything, the wireless light switch was working!

Arduino Code

Below is the complete code I used for this project.

The code constantly reads the value of analog input A0 and creates a running average of the last 10 readings. This is to filter out noise. Whenever the running average changes by more than my threshold, it’s considered a detection of an object and the output pin toggles.

Since the output pin is connected to the gate of the transistor, the transistor will turn on if the output is high, and off if the output is low. This turns the LED strip on and off.

const int numReadings = 10;
int readings[numReadings];      // Array to store the sensor readings
int readIndex = 0;              // Index of the current reading
int total = 0;                  // Running total of the readings
int average = 0;                // The average of the readings
int previousAverage = 0;        // The previous average value

const int ledPin = 7; // Pin that controls for the mosfet for the LED strip
const int threshold = 10;       // Threshold for average decrease
const int checkInterval = 500;  // Interval to check for decrease (ms)
unsigned long lastCheckTime = 0;// Last time when the decrease was checked

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

  // initialize the built-in LED pin as an output
  pinMode(ledPin, OUTPUT);

  // initialize all the readings to 0
  for (int i = 0; i < numReadings; i++) {
    readings[i] = 0;
  }
}

// the loop routine runs over and over again forever:
void loop() {
  // subtract the last reading from the total:
  total = total - readings[readIndex];

  // read the input on analog pin 0:
  readings[readIndex] = analogRead(A0);

  // add the reading to the total:
  total = total + readings[readIndex];

  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array, wrap around to the beginning:
  if (readIndex >= numReadings) {
    readIndex = 0;
  }

  // calculate the average:
  average = total / numReadings;

  // check the time interval
  if (millis() - lastCheckTime >= checkInterval) {
    if (previousAverage - average >= threshold) {
      // toggle the LED
      digitalWrite(ledPin, !digitalRead(ledPin));
    }
    // update the previous average
    previousAverage = average;
    // update the last check time
    lastCheckTime = millis();
  }

  // print out the average value:
  Serial.println(average);

  // delay in between reads for stability
  delay(10);
}

Demonstration Video

Watch the video below to see how the touchless lighting system works seamlessly with a wave of a hand.

Conclusion

I love it when I can combine what I love (electronics and circuits) with making something for my daughter.

And this touchless lighting project was a fantastic way to learn about a component I had very little experience with from before (the phototransistor) while also playing with the new Arduino UNO R4.

More Arduino Tutorials

Leave a Comment