Why 2nd PIR sensor is always HIGH? - arduino

I an getting a constant HIGH from 'inputPintwo' on the serial monitor. When 'inputPin' goes HIGH the relay is triggered and works properly because 'inputPintwo' is also HIGH (all the time).
I have a Very similar setup to: 2 PIR motion sensors +Arduino
I am not using pin 0 or 1 like the above answered question. I have replaced the sensor with a different one, in case it was bad hardware. I also unplugged the sensor and it still reads HIGH. The jumper is on retriggering on both sensors.
int ledPin = 13;
int inputPin = 2;
int inputPintwo = 4;
int pirState = LOW;
int val = 0;
int valtwo = 0;
#define RELAY1 7
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
pinMode(inputPintwo, INPUT);
pinMode(RELAY1, OUTPUT);
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin);
valtwo = digitalRead(inputPintwo);
if (val == HIGH && valtwo == HIGH) {
digitalWrite(ledPin, HIGH);
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
Serial.println("Light ON");
digitalWrite(RELAY1,1);
delay(500);
digitalWrite(RELAY1,0);
delay(500);
digitalWrite(RELAY1,1);
delay(500);
digitalWrite(RELAY1,0);
delay(500);
digitalWrite(RELAY1,1);
}
}
else {
digitalWrite(ledPin, LOW);
if (pirState == HIGH){
Serial.println("Motion ended!");
digitalWrite(RELAY1,0);
pirState = LOW;
Serial.println("Light OFF");
}
}
}
I expect both sensors to go HIGH only when motion is detected, which will cause the relay to go on and off several times, then stay on until the timer runs out on the sensors.

To identify the problem I recommend you to start with checking the hardware. You will need voltmeter/multimeter.
Double check if you are interfacing the sensor properly (check datasheet). Didn't you forget to connect e.g. the pull-down resistors?
Check power supply voltage on sensors – is the voltage within
manufacturer specifications?
Check breadboard connections if you are using one.
Check sensor output behaviour (voltage), if there is or is not a movement. Is the voltage constant or not? Constant voltage means that PIR sensor is NOT working properly. Before performing of this test disconnect output from Arduino input.
If everything seems OK or you do not have voltmeter, try to disconnect the PIR sensor and connect a wire between Arduino pin 4 and ground. Does digitalRead(inputPintwo) return LOW? If yes, you know that reading of the pin state works fine.
Below please see some recommendations related to your code:
use #define directive or static const int variable type to define Arduino pins as you do it with relay output pin RELAY1.
Example:
#define LED_PIN 13
#define INPUT_PIN 2
#define INPUT_PINTWO 4
or
static const int ledPin = 13;
static const int inputPin = 2;
static const int inputPintwo = 4;
In your case, where you are only interested in digital value (LOW/HIGH), use built pull-up resistor on the input pins. Thus the log. voltage level on the floating input pin is defined (HIGH). If you do not use pull-up resistors, voltage can be either log. 0 (LOW) or log. 1 (HIGH), what can lead to strange program/state machine behaviour
To activate pull-up resistors in the input pins, use
pinMode(inputPin, INPUT_PULLUP);
pinMode(inputPintwo, INPUT_PULLUP);

Related

NodeMCU doesn't seem to set digital pin low

I'm writing a simple project, using NodeMCU as my board. I have 2 analog devices: moisture sensor and brightness sensor. Since NodeMCU has only one analog pin, I try to power them in turns. To do so, I connect them to digital pins of NodeMCU. Digital pins output 3.3V 20-40mA in HIGH state (checked that with multimeter). That must be enough to power those devices. In the LOW state, according to Arduino IDE docs, voltage must be 0V (and that also was checked) and the sensor must not receive any current and, thus, must not output anything to A0. However, at the end I get correlated results: if I flash light at the photoresistor, humidity data is also affected. And vice-versa. How can I avoid it?
Here's the code, I'm using to do, what I described:
const int analogInputPin = A0; // Analog 0 (A0) on the board
// Several devices will use the same analog pin to send data
const int lightSensorResultPin = analogInputPin;
const int moistureSensorResultPin = analogInputPin;
const int lightSensorPowerPin = 16;
int lightSensorResult = 0;
const int moistureSensorPowerPin = 5;
int moistureSensorResult = 0;
void setup() {
Serial.begin(9600);
pinMode(lightSensorResultPin, INPUT);
pinMode(lightSensorPowerPin, OUTPUT);
digitalWrite(lightSensorPowerPin, LOW);
pinMode(moistureSensorResultPin, INPUT);
pinMode(moistureSensorPowerPin, OUTPUT);
digitalWrite(moistureSensorPowerPin, LOW);
}
void loop() {
/*==LIGHT SENSOR DATA READ BLOCK==*/
digitalWrite(lightSensorPowerPin, HIGH);
delay(1000);
lightSensorResult = analogRead(lightSensorResultPin);
digitalWrite(lightSensorPowerPin, LOW);
delay(1000);
/*==END OF LIGHT SENSOR DATA READ BLOCK==*/
/*==MOISTURE SENSOR DATA READ BLOCK==*/
digitalWrite(moistureSensorPowerPin, HIGH);
delay(1000);
moistureSensorResult = analogRead(moistureSensorResultPin);
digitalWrite(moistureSensorPowerPin, LOW);
delay(1000);
/*==END OF MOISTURE SENSOR DATA READ BLOCK==*/
Serial.print("Value on the light sensor: ");
Serial.println(lightSensorResult); // Light value. Low for bright
Serial.print("Value on the moisture sensor: ");
Serial.println(moistureSensorResult); // Moisture value. Low for wet
}
UPD: Here's the schematic
Instead of using digital outputs to switch on and off the different sensors, but combining their analog outputs, I would use an analog switch like 4066 to select which sensor you want to measure.

How to prevent an initial HIGH output in PIR Sensor

I am testing out the HC-SR501 PIR sensor on arduino. I tried a simple code tutorial online
int buzz = 13;
int pir = 2;
int value = 0;
int pirState = LOW;
void setup() {
pinMode(buzz, OUTPUT);
pinMode(pir, INPUT);
Serial.begin(9600);
}
void loop() {
delay(5000);
value = digitalRead(pir);
if (value == HIGH) {
digitalWrite(buzz, HIGH);
if (pirState == LOW) {
Serial.println("Motion Detected!");
pirState = HIGH;
}
} else {
digitalWrite(buzz, LOW);
if (pirState == HIGH){
Serial.println("Motion Ended!");
pirState = LOW;
}
}
}
This works, however, I'm trying to initialize it to a LOW output. When I first turn on the board, it initially gives me a high output, and so the buzzer activates instantly, even though I placed it away from myself. The serial prints out Motion Detected. I tried adding a delay, however it still gives out a HIGH output afterwards. Anyone knows how to solve this?
Thank you!
The pinMode sets the pin as an output, but the default state is LOW, so there should be no problem with it.
However, pin 13 is wired to onboard LED. And the onboard LED is also used by bootloader to signal its activity after the reset. You should check other pins but 13.

Arduino digital read is not working as expected

I have been using my Arduino to control motor polarity with a Cytron MD-10 motor driver shield, and I had it working.
Then when I came back to work it seemed as though the digital read pins were not differentiating between high and low voltage and one of the lights was powered on continuously.
The outputs function as set up with a continuous supply of measured 4.84 volts and then the switch is closed the voltage drops to 0 and the corresponding loop should be entered. Is my board fried?
Anything I should try?
const int outSwitch = 13;
const int inSwitch = 12;
const int pinPWM = 3;
const int pinDir = 2;
int lightOne = 11;
int lightTwo = 10;
static int Dir = 1;
static int cycleCounter = 0;
void setup() {
// Set Pin Out/Inputs:
pinMode(pinPWM, OUTPUT);
pinMode(pinDir, OUTPUT);
pinMode(outSwitch, INPUT);
pinMode(inSwitch, INPUT);
pinMode(lightOne, OUTPUT);
pinMode(lightTwo, OUTPUT);
analogWrite(pinPWM, LOW);
}
void loop() {
// Both read a low input value from the switch and then makes
// direction the opposite causing it to travel backwards:
if(digitalRead(inSwitch == LOW)){
analogWrite(pinPWM, HIGH);
digitalWrite(pinDir, Dir);
digitalWrite(lightOne, LOW);
digitalWrite(lightTwo, HIGH);
}
else if(digitalRead(outSwitch == LOW)){
analogWrite(pinPWM, HIGH);
digitalWrite(pinDir, -Dir);
digitalWrite(lightOne, LOW);
digitalWrite(lightTwo, HIGH);
}
}
Sma correctly identified the problem as a misplacement of the parentheses:
if (digitalRead(inSwitch == LOW)) // OOPS!
This compares inSwitch to LOW, which probably returns false. Since digitalRead expects an int, that false is converted to 0. So you do a read of pin 0. I don't think there is a pin 0, so you probably get an error. That return value is then implicitly compared to 0 in order to determine which branch of the if-statement to take.
The correct statement would look like this:
if (digitalRead(inSwitch) == LOW)
(Community Wiki because I'm just trying to provide a more detailed answer. I'm not trying to get rep for someone else's observation.)
What does analogWrite(pinPWM, HIGH); even mean? Missing a value (0-255), HIGH is not a valid value.

How do I program digital pins on arduino uno?

I'm trying to make a microcontroller with an arduino. I am supplying with +5volt from the arduino, sending it to an NC button (so i can manually decide when to output a certain timed pulse). After the button it goes to a pin that I have set as an inPin (pin8). Then I want the program to make pin 7 HIGH(with a delay), and then it goes to a transistor.
This is the code I tried making (I know almost nothing about coding):
int ledPin = 7;
int inPin = 8;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);
}
void loop()
{
if (inPin=HIGH) {
digitalWrite(ledPin, HIGH);
}
delay (500);
digitalWrite(ledPin, LOW);
}
For some reason the outPin is HIGH all the time. I remembered to hook up a resistor to GND so the digital pin would stay LOW when supposed to be LOW.
Thanks in advance!
if(inPin=HIGH) is a mistake, first of all use "==" instead of "=". ALso you need to READ input pin state: int invalue = digitalRead(inPin);
Also, all pins by default coonfigured as inputs, so you don't need use pinMode(inPin, INPUT);
After those changes your code will look like:
int ledPin = 7;
int inPin = 8;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (digitalRead(inPin)==HIGH) digitalWrite(ledPin, HIGH);
delay (500);
digitalWrite(ledPin, LOW);
}

NRF24L01 with ATTiny and Uno not connecting

I have an ATTiny85 connected to an NRF24L01+ module using this wiring diagram: diagram. The ATTiny85 periodically goes in and out of sleep to send some value to a receiver, an Arduino Uno. If the ATTiny is running off the Arduino power supply (3.3v), everything works correctly. When I run the ATTiny off of a separate CR2032 coin cell that delivers around 3v, the Arduino never receives any data. I have a status LED hooked up to the ATTiny to ensure that the ATTiny is waking correctly, which it is. Here's the code for both:
EDIT:
Connecting it to an external 3.3v not from the Uno makes everything work - why wouldn't the coin cell's voltage work? I think everything is rated below 2.8v, the CR2032 minimum.
ATTiny Code
#include <avr/sleep.h>
#include <avr/interrupt.h>
// Routines to set and claer bits (used in the sleep code)
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define CE_PIN 3
#define CSN_PIN 3 //Since we are using 3 pin configuration we will use same pin for both CE and CSN
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
byte address[11] = "SimpleNode";
unsigned long payload = 0;
void setup() {
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openWritingPipe(address); // Write to device address 'SimpleNode'
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(500);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(500);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(1000);
setup_watchdog(6);
}
volatile int watchdog_counter = 0;
ISR(WDT_vect) {
watchdog_counter++;
}
void loop()
{
sleep_mode(); //Go to sleep!
if(watchdog_counter >= 5)
{
digitalWrite(4, HIGH);
watchdog_counter = 0;
payload = 123456;
radio.write( &payload, sizeof(unsigned long) ); //Send data to 'Receiver' ever second
delay(1000);
digitalWrite(4, LOW);
}
}
//Sleep ATTiny85
void system_sleep() {
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System actually sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
}
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
WDTCR |= _BV(WDIE);
}
Receiver Code
#define CE_PIN 7
#define CSN_PIN 8
#include <SPI.h>
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
byte address[11] = "SimpleNode";
unsigned long payload = 0;
void setup() {
while (!Serial);
Serial.begin(115200);
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openReadingPipe(1, address); // Write to device address 'SimpleNode'
radio.startListening();
Serial.println("Did Setup");
}
void loop(void){
if (radio.available()) {
radio.read( &payload, sizeof(unsigned long) );
if(payload != 0){
Serial.print("Got Payload ");
Serial.println(payload);
}
}
}
Is the problem here that the ATTiny and Uno need to be turned on at the same time to establish a connection, or is it something to do with the battery, or something else entirely? Any help would be appreciated.
I'm experiencing the same problem when running Arduino Nano from a battery.
Nano has a 3.3V pin that I'm using for powering the NRF24L01+ module.
When the voltage from my battery-pack drops under 3.3V, the 3.3V pin voltage also drops. After few minutes, the RF module is not sending any messages.
I fixed the problem temporarily by routing the battery through a 12V step-up regulator that I bought earlier for a different project. These 12V then go to the "UN" pin on Nano which accepts 6-20V. This setup works nicely but is definitely not optimal.
Therefore I'm planning to use a 3.3V step-up regulator such as Pololu 3.3V Step-Up Voltage Regulator U1V11F3 which, according to the supplier, can efficiently generate 3.3V from input voltages as low as 0.5V.
I think this might be helpful to your project as well.

Resources