Stuck with simple motor circuit

I am trying to build a really simple circuit in which a dc motor is controlled by a spdt slide switch. The programme seems fine and I can get it to run - trouble is, I can't get the switch to control the motor switching on and off. Any clues? I'm probably doing something stupid! Thanks.

int switchA = 7; int motorA = 2;

void setup(){ pinMode(switchA,INPUT); pinMode(motorA,OUTPUT); }

void loop(){ if(switchA,HIGH){ digitalWrite(motorA,HIGH); delay (1000); digitalWrite(motorA,LOW); delay(2000); } else{ digitalWrite(motorA,LOW); } }

https://123d.circuits.io/circuits/1199570-int-2-2013-q3/edit

Hopefully this link takes you to the 123d simulation circuit.

Debs

DON't connect a motor directly to an Arduino pin. That will damage the pin and/or the Arduino. Pins are only rated for 20mA (40mA absolute max).

Motors should be switched with a suitable resistor/transitor circuit with back EMF diode.

It seems you also have connected a 9volt battery to Arduino's 5volt pin. That will smoke your Arduino.

Connect the switch to an input pin and ground. No resistor. Enable the internal pullup resistor in pinMode. https://www.arduino.cc/en/Reference/PinMode

Read this. https://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors/transistors Leo..

Thankyou. I know my circuit was non-existent but it seemed to be ok without the transistor - not that I would have built it that way! Anyway, it turned out that I had an error with my code. If I changed if(digitalRead(switchPin,HIGH) to ifdigitalRead(switchPin ==HIGH) the circuit behaved properly. I assume that the first statement set the switchPin to HIGH all of the time - which is why switching on/off made no difference and the second conditional statement tests the condition of the pin to see if it is HIGH or not.

Debs

PS I have now constructed it with the transistor switching circuit too. thanks again for your help.

debkeys: Thankyou. I know my circuit was non-existent but it seemed to be ok without the transistor - not that I would have built it that way! Anyway, it turned out that I had an error with my code. If I changed if(digitalRead(switchPin,HIGH) to ifdigitalRead(switchPin ==HIGH) the circuit behaved properly. I assume that the first statement set the switchPin to HIGH all of the time - which is why switching on/off made no difference and the second conditional statement tests the condition of the pin to see if it is HIGH or not.

Debs

PS I have now constructed it with the transistor switching circuit too. thanks again for your help.

But still, you should not power the motor from the +5 Volts from the Arduino. Motors should be powered by a separate power supply and a common ground.

ifdigitalRead(switchPin ==HIGH)

That line is missing a lot of brackets. It should look more like:

if(digitalRead(switchPin) ==HIGH)

The closing bracket after switchPin is really important. switchPin==HIGH is actually a valid pin number, but probably not the pin you want.

debkeys: Thankyou. I know my circuit was non-existent but it seemed to be ok without the transistor - not that I would have built it that way! Anyway, it turned out that I had an error with my code. If I changed if(digitalRead(switchPin,HIGH) to ifdigitalRead(switchPin ==HIGH) the circuit behaved properly. I assume that the first statement set the switchPin to HIGH all of the time - which is why switching on/off made no difference and the second conditional statement tests the condition of the pin to see if it is HIGH or not.

Debs

PS I have now constructed it with the transistor switching circuit too. thanks again for your help.

No, the code you need is

  if (digitalRead (switchPin))

digitalRead takes a single argument the pin number, and returns a single truth value (HIGH and LOW are synonyms for true/false and 1/0)

Don't connect a motor to an Arduino pin, the pin can't handle the current nor the inductive voltage spikes, and will fail.