Peter Javorsky
Published © CC BY

Knock controlled night light

This is a very simple circuit to turn on/off a white LED strip powered from old 12V 1A wall adapter by knocking. Knock sensor is a piezo.

IntermediateFull instructions provided2,022
Knock controlled night light

Things used in this project

Hardware components

Maple Mini
Arduino compatible - see www.stm32duino.com
×1
Piezo
×1
TIP102
Power NPN darlington transistor
×1
Resistor 1M
1 Megaohm resistor
×1
Resistor 1k
1 kiloohm resistor
×1
Wires
Some wires to wire it up all together
×1

Software apps and online services

Arduino IDE
Arduino IDE
Needs some hacking thought - see www.stm32duino.com

Schematics

Schematic - knock controlled LED strip

Code

Knock detector

C/C++
This program detects knocks on piezo (pin 11) and turns on/off the LED strip via TIP102 on pin 3.
/* *************************************

Project name: LED strip control by knock
Author: Tekk (Peter Javorsky)
Date: 24/9/2015
Contact: tekk.sk (at) gmail.com

**************************************** */

int ledStripPin = 3;  // this controls the TIP102 and turns on/off the LED strip
int knockSensor = 11; // this is where your piezo is connected, don't forget the 1 Megaohm resistor in parralell
int statePin = LOW;
int THRESHOLD = 27; // needs to be adjusted according to your piezo type!

void setup() 
{
 pinMode(ledStripPin, OUTPUT); 
 Serial.begin(9600);
 delay(100);  // wait for A/D converter and/or piezo to stabilize
}

void loop() 
{
  delay(1);  // don't remove this delay - Sample size: 1ms
  
  if (analogRead(knockSensor) >= THRESHOLD) 
  {
    statePin = !statePin;
    digitalWrite(ledStripPin, statePin);
    Serial.println("Knock!");
    delay(50);  // wait a while for piezo to stabilize
  }
  
}

Double-knock activated (version 2)

C/C++
/* *************************************

Project name: LED strip control by knock
Author: Tekk (Peter Javorsky)
Date: 24/9/2015
Contact: tekk.sk (at) gmail.com

**************************************** */

int ledStripPin = 3;  // this controls the TIP102 and turns on/off the LED strip
int knockSensor = 11; // this is where your piezo is connected, don't forget the 1 Megaohm resistor in parralell
int statePin = LOW;
int THRESHOLD = 27; // needs to be adjusted according to your piezo type!

void setup() 
{
 pinMode(ledStripPin, OUTPUT); 
 Serial.begin(9600);
 delay(100);  // wait for A/D converter and/or piezo to stabilize
}

void loop() 
{
  delay(1);  // don't remove this delay - Sample size: 1ms
  
  if (analogRead(knockSensor) >= THRESHOLD) 
  {
    if (!statePin)
    {
      unsigned long t = millis() + 350;
      delay(50); // debounce only
      
      while (millis() < t)
      {
        delay(1);  // don't remove this delay - Sample size: 1ms
        
        if (analogRead(knockSensor) >= THRESHOLD) 
        {
          statePin = !statePin;
          digitalWrite(ledStripPin, statePin);
          Serial.print("Double ");
          delay(50);  // wait a while for piezo to stabilize
          break;
        }
      }
    }
    else
    {
      statePin = !statePin;
    }
    
    digitalWrite(ledStripPin, statePin);
    Serial.println("Knock!");
    delay(50);  // wait a while for piezo to stabilize
  }
  
}

Credits

Peter Javorsky

Peter Javorsky

4 projects • 14 followers
.NET C# Linux Arduino Embedded RPi

Comments