Hi, I have a problem with digitalRead() on a 12v power supply. If I connect a 12v (or 5v phone charger) then all digitalRead() are HIGH, but if I connect the arduino to the computer it works fine. What's the problem?
My code:
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
int sensor = 2;
int val = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int t = 0;
int s = 1;
int ts = 0;
// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
int pwmn = 0;
int pwmn1 = 0;
void setup() {
pinMode(sensor,INPUT);
pwmn = 0;
pwmn1 = 0;
a = 0;
b = 0;
c = 0;
d = 0;
t = 0;
Serial.begin(9600);
Serial.println("16 channel PWM test!");
pwm.begin();
pwm.setOscillatorFrequency(27000000); // The int.osc. is closer to 27MHz
pwm.setPWMFreq(1600); // This is the maximum PWM frequency
Wire.setClock(400000);
}
void loop() { // read sensor value
if (s == 1){
if (digitalRead(sensor) == HIGH) { // check if the sensor is HIGH
Serial.println("sensor");
a = a+1;
s = 0;
}
}
schg1();
schg0();
timersensor();
}
void schg1() {
if (a == 1){
for(int i=0; i<4096; i += 10){
if (i >= 4050){
if (pwmn >= 16){
a = 0;
} else {
pwmn = pwmn+1;
i = 0;
if (pwmn >= 4){
b = 1;
}
}
} else {
Serial.println(pwmn);
pwm.setPWM(pwmn, 0, i);
}
}
} else {
a = 0;
}
}
void schg0() {
if (b == 1){
Serial.println("b = 1");
for(int i1=4095; i1>0; i1 = i1 - 5){
if (i1 <= 5){
if (pwmn1 >= 16){
pwmn = 0;
a = 0;
ts = 1;
b = 0;
} else {
pwmn1 = pwmn1+1;
i1 = 4095;
}
} else {
Serial.println(pwmn1);
pwm.setPWM(pwmn1, 0, i1);
}
}
}
}
void timersensor(){
if (ts == 1){
delay(10000);
s = 1;
ts = 0;
}
}
void schd1() {
}
void schd0() {
}
Yes, but how is it wired ?
What is connected to pin 2 when the switch is open ?
What is connected to pin 2 when the switch is closed ?
Do you have a pull up or pull down resistor in place to hold the pin in a known state all the time or is it floating at an unknown, possibly HIGH, possibly LOW voltage ?
"Try check ground connections too (every GND has to be together), maybe its not wired very well (when you put an external power source)" All GND are well wired.
I noticed that only after connecting devices communicating in some way with arduino it works correctly (pc, laptop). Error in the code? What could be the problem?
"Do you have a pull up or pull down resistor..." No
Unless held at a specific voltage the input pin could be at any level. Adding a resistor to hold the pin at a known level, say LOW, until pressing the button takes it HIGH is one solution, although it requires a component and more wiring
A more elegant solution is to use INPUT_PULLUP as the parameter for the pin in pinMode() which causes the internal pullup resistor to be activated to hold the pin normally HIGH. You also need to change the wiring to cause the switch to take the pin to GND when activated and change the program logic to detect the LOW input when the switch is activated.
UKHeliBob:
Unless held at a specific voltage the input pin could be at any level. Adding a resistor to hold the pin at a known level, say LOW, until pressing the button takes it HIGH is one solution, although it requires a component and more wiring
A more elegant solution is to use INPUT_PULLUP as the parameter for the pin in pinMode() which causes the internal pullup resistor to be activated to hold the pin normally HIGH. You also need to change the wiring to cause the switch to take the pin to GND when activated and change the program logic to detect the LOW input when the switch is activated.
The first thing to check is whether the PIR is causing the problem in the first place. Can you please provide a link to the device that you are using ? A properly designed unit should keep the output at a known state (HIGH or LOW) at all times and you almost certainly cannot change its behavior.
One thing that you could test is whether the PIR behaves as expected in all power supply modes by writing a simple sketch that just reads its output value and prints it to the Serial monitor
Incidentally, why is the PIR powered from the IOREF pin of the Uno rather than 5V ?
12Stepper:
Probably nothing to do with OP's problem, but I'm wondering about using IOREF as a 5V source. I know it's 5V, but does it have any current ability?
UKHeliBob:
The first thing to check is whether the PIR is causing the problem in the first place. Can you please provide a link to the device that you are using ? A properly designed unit should keep the output at a known state (HIGH or LOW) at all times and you almost certainly cannot change its behavior.
One thing that you could test is whether the PIR behaves as expected in all power supply modes by writing a simple sketch that just reads its output value and prints it to the Serial monitor
Incidentally, why is the PIR powered from the IOREF pin of the Uno rather than 5V ?
If I don't have pir connected to arduino, also digitalread() is HIGH.
PIR = HC-SR501