2

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

enter image description here

11
  • 1
    checked, the motor and the button is wired correctly ... how do you know?
    – jsotola
    Commented Dec 23, 2020 at 2:55
  • 1
    I strongly suspect a hardware issue. How are you providing power to the Mega2560 and the motor? How are you controlling the motor and how is the button wired (pulldown)? You are aware that, with the code you've shown, once you activate the motor output, it will never turn off?
    – StarCat
    Commented Dec 23, 2020 at 9:38
  • Please show your wiring. You say the wiring is not bad, but we'll be the judge of that, not you.
    – Majenko
    Commented Dec 23, 2020 at 11:29
  • 1
    There's also nothing to pull the button output to 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.
    – StarCat
    Commented Dec 23, 2020 at 15:53
  • 1
    See here The resistor needs to be between the button input pin and ground to give it a defined value when the button is released. The resistor to 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.
    – StarCat
    Commented Dec 23, 2020 at 16:02

2 Answers 2

1

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.

1

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.

3
  • Well, the problem here is, that it doesn't even read the inputs correctly. So regardless of the motor, it still won't function. Commented 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
    Commented 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". Commented Dec 23, 2020 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.