I am using an Arduino Atmega2560 clone, I have used it many times before with satisfactory results - although I haven't used it with my Rpi before.
My Raspberry Pi is model 4 (B I think?).
#define knap 49
#define motorOutput 8
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(knap, INPUT);
pinMode(motorOutput, OUTPUT);
digitalWrite(motorOutput, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
int knapSensor = digitalRead(knap);
Serial.print(knapSensor);
if (knapSensor == 1) {
int knapSensor = digitalRead(knap);
Serial.print("aktiveret");
digitalWrite(motorOutput, HIGH);
delay(500);
digitalWrite(motorOutput, LOW);
}
}
Now I have double checked, the motor and the button is wired correctly. There is no hardware issue, aside from the cables being sh- quality.
Problem is, whenever I run this program two things will happen;
Either it starts fine by displaying "0000..." on the serial monitor.
Then when I press the button it goes to "AktiveretAktiveretAktiveret..." even when I depress the button.
Or, it will just display "AktiveretAktiveretAktiveret...".
In either case the motor won't work.
Once again, I have done tests where I have removed the power from the breadboard and replaced the voltage pin for the motor with the voltage pin leading from the button.
First thing didn't change the input (i.e still "AktiveretAktiveret,") the latter started the motor.
I.e this isn't about bad wiring.
EDIT:
Added schematics and changed the code
2 Answers 2
Your button needs a pulldown resistor to work reliably.
See the discussion and solution here
The resistor, with a value of around 10K (but any value between 3K and 20K will probably do), needs to be between the input pin or your Arduino and ground to give it a defined value when the button is released.
Putting a resistor between the Arduino pin and ground will make sure the pin reads "low" when no button is pressed. When the button is pressed, it will be connected directly to +5V and will read high. Only a small current (0.5mA) will flow from +5V to ground through the 10K Ohm resistor in that case. It will not create a short circuit.
What motor are you using? If it works straight from the 5v but not from a GPIO, it could be an issue of insufficient power.
As @StarCat4 mentioned, you also need to add a digitalRead inside your while loop if you want the motor to ever turn off.
-
Well, the problem here is, that it doesn't even read the inputs correctly. So regardless of the motor, it still won't function.GeorgeWTrump– GeorgeWTrump2020年12月23日 14:50:23 +00:00Commented Dec 23, 2020 at 14:50
-
It reads when you press the button in, correct? You'll never see a state change after pressing in the button because you haven't instructed the Arduino to look for it. Even if you release the button, the Arduino does not know to check state. Your while loop should read
while (knapSensor == 1){ Serial.print("aktiveret"); digitalWrite(motorOutput,HIGH); delay(500); knapSensor = digitalRead(knap); }
Andrew– Andrew2020年12月23日 15:10:13 +00:00Commented Dec 23, 2020 at 15:10 -
Oops, forgot to edit the code. It will occasionally print out "Aktiveret" and then go "0000...." i.e "000001Aktiveret " is the default pattern, when I am not pressing the button. When I press the button it will go complete "00000".GeorgeWTrump– GeorgeWTrump2020年12月23日 15:41:26 +00:00Commented Dec 23, 2020 at 15:41
Explore related questions
See similar questions with these tags.
checked, the motor and the button is wired correctly
... how do you know?LOW
when the button is released so it is very vulnerable to noise. You should put a pulldown resistor of around 10K Ohms between pin 49 and ground to make the button more reliable.