I have written a very simple code which takes an RCWL0516 Microwave Sensor and uses the output to digitalWrite a HIGH or LOW to the internal LED on the Arduino Mega.
I have also tried to output a message in the serial monitor. However, I am only displaying the ELSE statements comments and not the comment in the IF statement.
Any advice would be greatly appreciated.
//Testing the microwave sensor with a simple LED
#define RadarPinNumber 2
//int PIRPinNumber = 2; //defined above
int RadarState = LOW;
int RadarVal = 0; //Used to read the value (status) of the sensor
void setup() {
//Enable data logging
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
RadarVal = digitalRead(RadarPinNumber); //Reading the value of the PIR status
if (RadarVal == HIGH){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
RadarState = LOW;
if (RadarState = LOW){
Serial.print("Motion has been detected");
Serial.print('\n');
Serial.print('\n');
RadarState = HIGH;
}
}
else {
digitalWrite(LED_BUILTIN, LOW); // turn the LED off (LOW is the voltage level)
Serial.print("No Motion");
Serial.print('\n');
Serial.print('\n');
RadarState = LOW;
}
}
How IS the switch wired? Using INPUT_PULLUP as the mode (and actually calling pinMode()) makes wiring so much easier.
if (RadarVal == HIGH){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
RadarState = LOW;
if (RadarState = LOW){
Did you happen to put a low or high on the CDS pin of the micro wave motion detector? It s best to just leave CDS floating. I found that trying to use CDS as a control for the MWMD (micro wave motion detector) was a folly on my part.
However, the problem wasn't with the sensor not reading a value... it was with the output not going to Serial Monitor. I gave it a gander, and thought about the ambiguous comment from
PaulS:
One out of two IS bad...
and then looked again. I was missing the second equality sign. The code now outputs as expected. Below is the new setup:
//Testing the microwave sensor with a simple LED
// Future goal is to connect to an LED Strip as well as the web and create an interface for manual input
// Possibly enable Alexa services as well.
// Setting up variables for the PIR sensor
// Send 5V to 5V on Arduino, and Ground to Ground
// Signal pin can be used as a logic level high and low
// no functions need to be defined
// We will monitor the state of the pin, since logging a high or low might result in a false positive
int RadarPinNumber = 2;
int RadarState = LOW;
int RadarVal = 0; //Used to read the value (status) of the sensor
void setup() {
//Enable data logging
Serial.begin(9600);
// pinMode(LED_BUILTIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(RadarPinNumber, INPUT_PULLUP);
// pinMode(RadarPinNumber, INPUT);
}
void loop() {
RadarVal = digitalRead(RadarPinNumber); //Reading the value of the PIR status
if (RadarVal == HIGH){
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
RadarState = LOW;
if (RadarState == LOW){
Serial.print("Motion has been detected");
Serial.print('\n');
Serial.print('\n');
RadarState = HIGH;
}
}
else {
digitalWrite(LED_BUILTIN, LOW); // turn the LED off (LOW is the voltage level)
Serial.print("No Motion");
Serial.print('\n');
Serial.print('\n');
RadarState = LOW;
}
}
Idahowalker:
Did you happen to put a low or high on the CDS pin of the micro wave motion detector?
I have yet to dive into the CDS Pin as of now. Soon I will do more poking around.
outsider:
Is the 3.3V output from the radar high enough for a solid HIGH on the Mega pin? Might try analog input:
The specs for the 328P indicate that the 'lowest value where the pin is guaranteed to be read as high' is 0.6Vcc. While 3.3V logic is usually handled okay by TTL (5V), you are walking a thin line. If, as Paul suggested, the pin is configured with a pull-up, it becomes a non-issue.