Motion sensor being ignored in wireless motion/light sensor --> 220v lamp

Hello there,

What I’m trying to make:
A wireless motion sensor (with a photoresistor) that turns on an 220v AC lamp.

The problem:
I would like the lamp to be turned on when there is both motion and it reaches a certain light level. The hardware setup seems to work but for some reason it turns on from only reaching the light threshold without detecting motion (pir motion sensor completely turned away from me and I’m straight behind it while holding my hand over the photoresistor).

Hardware that I’m using:

PIR motion sensor module
Photoresistor stuck in the breadboard with a resistor
Arduino Nano
RF transmitter
RF receiver
Arduino Uno (will become a Nano as well)
Single relay module connected to 220v light cable with an LED bulb

This is my first own project after a few of the Arduino starter projects and I’ve been gathering and learning code bit by bit (from blink to motion sensor, photoresistor, rf transmitterand and adding a relay to connect a 220v lamp) trying to get the whole setup to work.

I have no previous coding experience but have been trying out combining and changing bits of the code after some googling but think I need some extra eyes and experience here, since I’m not sure which parts of the code I should or shouldn’t keep.

The parts I understand the least myself of this code is the parts for the RF transmitter/receiver. I think the command to turn the lamp on was actualy the confirmation signal that a message was received by the RF receiver from the RF transmitter and letting that (for now) last for 5 seconds.

This is the code of the sensor and transmitting part:

// Include VirtualWire library
#include <VirtualWire.h>
 
// Define pins
const int led_pin = 13;
const int transmit_pin = 12;
const int sensor_pin = 7;
const int photo_pin = A0;
const int threshold = 975;
 
int sensor_value;
 
void setup()
{
  Serial.begin(9600); 
  // Init
   vw_set_tx_pin(transmit_pin);
   vw_setup(2000); // Transmission rate
   pinMode(led_pin, OUTPUT);
   pinMode(sensor_pin,INPUT);
}
 
void loop()
{
  Serial.println(analogRead(photo_pin)); 
  
  // Get motion sensor value
   sensor_value = digitalRead(sensor_pin);
 
   // Init message
   char msg[3] = {'0'};
 
   // Change message if motion is detected
   if (sensor_value == 1 && analogRead(photo_pin) > threshold){
    
   // Transmit data every second
   digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
   vw_send((uint8_t *)msg, 3);
   vw_wait_tx(); // Wait until the whole message is gone
   digitalWrite(led_pin, LOW);
   delay(1000);
     }
}

And this is my receiving end:

// Include VirtualWire library
#include <VirtualWire.h>
 
// Pins definition
const int relais_pin = 13;
const int transmit_pin = 12;
const int receive_pin = 11;
 
void setup()
{
   // Init
   delay(1000);
   Serial.begin(9600); // Debugging only
   Serial.println("setup");
 
   // Initialise the IO and ISR
   vw_set_rx_pin(receive_pin);
   vw_setup(2000); // Transmission rate
 
   // Start the receiver PLL
   vw_rx_start();
 
   // Set relais pin
   pinMode(relais_pin, OUTPUT);
   digitalWrite(relais_pin, LOW);
}
 
void loop()
{
   uint8_t buf[VW_MAX_MESSAGE_LEN];
   uint8_t buflen = VW_MAX_MESSAGE_LEN;
 
   // Check if a message was received
   if (vw_get_message(buf, &buflen))
   {
     // Flash a light to show received good message
   digitalWrite(relais_pin, HIGH);
   delay(5000);
   Serial.print("Got: ");
 
   // Print message
   for (int i = 1; i < buflen; i++)
   {
     Serial.print(char(buf[i]));
     Serial.print(' ');
   }
   Serial.println();
   digitalWrite(relais_pin, LOW);
   }
}

Before I added the relay and went to 220v I had an LED on pin 13 and it worked. Though there I dind’t have the if…&& photo_pin… statement yet in the transmitting part but used an if statement within the if statement. When it didn’t work in the setup where I changed the small LED for a relay with a LED light bulb I read somewhere on the forum that the && within an if statement should be better, but no luck.

I hope I’m being complete enough, if a picture of my hardware has to be added let me know but since the lamp is working but already responds when the photo resistor threshold is met without motion being sensed I’m assuming the problem is in (the transmitter part of) my code.

So I hope there is some simple mistake, probably from not understanding the code enough while putting it together from different seperate pieces of code I found online, that makes the lamp turn on from only the light threshold while ignoring the motion sensor.

Thanks a lot for reading this long message and if I missed giving any important information please let me know! Hope someone can help because I’m curious to learn where I messed up;).

Try to get one thing to work at a time.

Put an LED on the nano with the motion sensor, so when it senses motion it blinks, or something.

If you see it blinking, but the lamp doesn't come one, that suggests a communication problem.

But if it never blinks, then the problem is with the motion sensor or the relevant part of your code.

// Change message if motion is detected if (sensor_value == 1 && analogRead(photo_pin) > threshold){

This is only going to work if both your criteria are satisfied.

It also doesn't make much sense to turn the lamp on and off every second.

Your sending program needs to remember whether it should be on or off, and only send a message when it changes.

It also seems to me, your message should contain some content which means, to turn it on, or turn it off.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom..... :)