Project tutorial
Neopixel Motion Activated Night Light

Neopixel Motion Activated Night Light

Neopixels make a moving rainbow when triggered by a distance less than 1 meter in front of an Ultrasonic sensor.

  • 3,212 views
  • 4 comments
  • 10 respects

Components and supplies

About this project

It's finally working! Took so many code rewrites.

Background

I am new to all of this open source electronics DIY stuff. I am a moderator for Hackster.io, and as a result, have become interested in the projects I review. I decided to make a motion activated night light for my bathroom. This way I don't have to turn on the bathroom light in the middle of the night, blinding myself every time go tinkle.

Introduction

I grabbed code from a few sources and compiled them together to make the attached code. With Ingo Lohs help, I saw where I was making a mistake. I was doing strip.clear, but not strip.show afterwards. Now it works like a beauty. I used hardware that I had laying around. No motion sensor was available, hence the Ultrasonic sensor.

Steps

  • connect the Ultrasonic sensor Grd to UNO Grd
  • solder wires to  Neopixel Grd, 5v, and Din
  • plug Neopixel Grd wire to Grd on UNO
  • join and solder Neopixel 5v wire and Ultrasonic sensor vcc to resistor to UNO 5v
  • connect Ultrasonic sensor Trig to UNO pin 2
  • connect Ultrasonic sensor Echo to UNO pin 3
  • connect Neopixel Din to UNO pin 12
  • Upload sketch

Summary

After a few easy connections to the UNO, you have a pretty night light that will activate when you walk in front of the Ultrasonic sensor. I hope this has helped those of you that want to use an Ultrasonic sensor as a motion detector. Adjust the distance for activation by altering the "if" loop cm = _____.

Code

Code that works!Arduino
#include <Adafruit_NeoPixel.h>
#define LEDPIN 12 // connect the Data from the strip to this pin on the Arduino
#define NUMBER_PIEXELS 43// the number of pixels in your LED strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMBER_PIEXELS, LEDPIN, NEO_GRB + NEO_KHZ800);
const int TrigPin = 2;
const int EchoPin = 3;
float cm;
int delayTime = 200;
int wait = 500;
int i = 0;


void setup()
{
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
strip.begin();

}
void loop()
{

digitalWrite(TrigPin, LOW);       
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
cm = pulseIn(EchoPin, HIGH) / 58.0; //The echo time is converted into cm
cm = (int(cm * 100.0)) / 100.0;     //Keep two decimal places
Serial.print("Distance\t=\t");
Serial.print(cm);
Serial.print("cm");
Serial.println();
    
 if (cm <100){

for (int i=0; i<strip.numPixels()-1; i++) {
   // starting at i, draw the 7 color rainbow}
   // a seven segment rainbow with red on the highest pixel 
void rainbow7(uint16_t i, uint16_t wait) ;
    int np = strip.numPixels();  // we use the modulo function with this
    strip.setPixelColor(i     % np, 0, 0, 0); // off
    strip.setPixelColor((i+1) % np, 25, 0, 25); // violet
    strip.setPixelColor((i+2) % np, 255, 0, 255); // indigo
    strip.setPixelColor((i+3) % np, 0, 0, 150); // blue
    strip.setPixelColor((i+4) % np, 0, 150, 0); // green
    strip.setPixelColor((i+5) % np, 255, 255, 0); // yellow
    strip.setPixelColor((i+6) % np, 110, 70, 0); // orange
    strip.setPixelColor((i+7) % np, 150, 0, 0); // red
    strip.show();
    delay(wait); 
    strip.clear();
    strip.show();}}
  else{
  strip.clear();
  strip.show(); 
  }

}

Schematics

Good luck figuring this schematic out. Sorry.
terrible_schematic_for_neopixel_motion_sensor_KRoUGiFmGO.fzz

Comments

Similar projects you might like

Motion Activated Night Light

Project tutorial by Haziq Afandi

  • 3,114 views
  • 0 comments
  • 4 respects

RobotGeek PIR Motion Sensor Night Light

Project tutorial by Team RobotGeek Projects Team

  • 1,502 views
  • 1 comment
  • 6 respects

LEGO-Compatible Night Light

Project tutorial by Lunchbox Electronics

  • 2,670 views
  • 2 comments
  • 18 respects

Night Light Assistant

Project showcase by Gabriele Fugazzi

  • 3,830 views
  • 9 comments
  • 21 respects

Motion Detection Lamp

Project tutorial by Izzati Azryna

  • 3,063 views
  • 0 comments
  • 8 respects

My Motion Activated Security Cam project

Project tutorial by Christiaan Neil Burger

  • 9,231 views
  • 9 comments
  • 12 respects
Add projectSign up / Login