John Shermanali matar
Published

Motion detected light switch

Have you ever wanted to control a linear actuator with sensors? Well, we did, and we used the actuator to control a light switch.

BeginnerFull instructions provided254
Motion detected light switch

Things used in this project

Hardware components

Argon
Particle Argon
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
ELEGOO 120pcs Multicolored Dupont Wire
ELEGOO 120pcs Multicolored Dupont Wire
×1
Grove - 2-Channel SPDT Relay
Seeed Grove - 2-Channel SPDT Relay
×1
ObjectBlocks Mini linear actuator
×1
LED (generic)
LED (generic)
×1

Story

Read more

Schematics

Actuator Circuit

PIR Circuit

Code

PIR code

C/C++
int Sensor =D0;
int MOVEMENT = 0;

void setup() {
  
    pinMode(D9,OUTPUT);
    pinMode(Sensor,INPUT);
    Particle.subscribe("received",rec);
}

void loop() {
  
    MOVEMENT = digitalRead(Sensor);
    
    if (MOVEMENT == HIGH) {
        Particle.publish("Motion_detected", String(MOVEMENT));
        delay (50000);
    }

}

void rec(String event, String data){
    digitalWrite(D9, HIGH);
    delay(500);
    digitalWrite(D9, LOW);
}

Actuator code

C/C++
void setup() {
    Particle.function("3171lightactivated", lightswitch);
    Particle.subscribe("Motion_detected",on);
    pinMode(D0, OUTPUT);
    pinMode(D1, OUTPUT);
}

int lightswitch(String command) {
    if(command == "off") 
    {
        digitalWrite(D1, HIGH);
        digitalWrite(D0, LOW);
        return 1;
    }
    else if(command == "on")
    {
        digitalWrite(D0, HIGH);
        digitalWrite(D1, LOW);
        return 2;
    }
    else
    {
        digitalWrite(D1, LOW);
        digitalWrite(D0, LOW);
        return 3;
    }
}

void on(String event, String data){
    digitalWrite(D0, HIGH);
    digitalWrite(D1, LOW);
    delay (10000);
    digitalWrite(D0, HIGH);
    digitalWrite(D1, HIGH);
    delay (10000);
    digitalWrite(D1, HIGH);
    digitalWrite(D0, LOW);
    delay (10000);
    digitalWrite(D0, LOW);
    digitalWrite(D1, LOW);
    Particle.publish("received");
}


void loop() {

}

Credits

John Sherman

John Sherman

1 project • 0 followers
ali matar

ali matar

0 projects • 1 follower

Comments

Add projectSign up / Login