I have a Leonardo with a Wireless SD Shield on top of it and an XBee on the shield. XBee communication works perfect, I can make the built in LED on the Leonardo blink from a distance by setting pin 13 as output and changing between digitalWrite high and low.
I wanted to attach a little motor to it. So I attached the motor to the 5V and GND on the Wireless Shield (which is attached to the Leonardo. The motor started spinning. I tried the same with the 3.3V and that worked too. Now I wanted to be able to start it from a distance so I changed the 13 to 2 in my code and reuploaded the sketch. I attached to motor to the 2 and the GND. When sending the EXACT same signal as before, the motor didn't do anything at all.
Anyone that can help me? Is it the Wireless SD Shield interfering? Or me forgetting something?
I already did that and didn't see anything. I have four commands programmed, when they get sent over the XBees the led blinked perfectly like expected. I changed the 13 to a 2 as you see in the code below and the motor does not respond.
#include <XBee.h>
int teller = 1;
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
Rx16Response rx16 = Rx16Response();
Rx64Response rx64 = Rx64Response();
char data[10];
char FORWARD = 'F';
char BACKWARD = 'B';
char LEFT = 'L';
char RIGHT = 'R';
char led = 2;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial1.begin(9600);
xbee.setSerial(Serial1);
//while (!Serial) {
//}
}
void loop() {
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
if (xbee.getResponse().getApiId() == RX_16_RESPONSE || xbee.getResponse().getApiId() == RX_64_RESPONSE) {
char reset[10];
data[0] = reset[0];
String payload = "";
Serial.println();
Serial.print(teller);
teller++;
Serial.print(". ");
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
xbee.getResponse().getRx16Response(rx16);
for (int i= 0; i < rx16.getDataLength(); i++){
if (iscntrl(rx16.getData()[i]))
Serial.write(' ');
else
Serial.write(rx16.getData()[i]);
}
Serial.println();
data[0] = rx16.getData()[0];
}
else {
xbee.getResponse().getRx64Response(rx64);
for (int i= 0; i < rx64.getDataLength(); i++){
if (iscntrl(rx64.getData()[i]))
Serial.write(' ');
else
Serial.write(rx64.getData()[i]);
}
Serial.println();
data[0] = rx64.getData()[0];
}
// todo na data afscheiden
if (data[0] == FORWARD ){
Serial.write("Rij naar voor.");
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
}
if (data[0] == BACKWARD){
Serial.write("Rij naar achter.");
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(200);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
}
if (data[0] == LEFT){
Serial.write("Stuur naar links.");
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(200);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(200);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
}
if (data[0] == RIGHT){
Serial.write("Stuur naar rechts.");
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(200);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(200);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(200);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
}
}
else {
// did not receive correct response
}
}
else if (xbee.getResponse().isError()) {
// error (no response?)
}
}
An Arduino output pin can only source 40 mA without damage (20-30 mA is a reasonable safe limit). This means that they can't drive any but the smallest of motors (like a cellphone vibrator motor). For anything larger than that you would generally use a transistor switch:
iPot:
How would you explain the fact that it does work on the 3.3V and 5V output pin of the board? Do they give more amps?
Yes. The 5V pin can supply over 400 mA on USB and over 900 mA when externally powered with 7-12V. Trying to draw more than 40 mA from an output pin can damage the output transistors.
Sol what you're telling me is to use a transistor as a switch while having the motor connected to the 5V or 3.3V so it can be turned on and if. And connect the transistor to pin 2 in my case.
I'm fairly new to electronics sorry if I'm being a pain.
It's not a good idea to power a motor from the Arduino 5v pin because they can cause the Arduino's inernal 5v supply to drop causing the Arduino to reset - often with very strange symptoms that are easily confused with software errors. If it is a very small motor with no load on it you may get away with it but it would be a good idea to give the motor it's own power supply controlled, as others have said, through a transistor or a proper motor driver board.
So I should connect the B to pin 2 in my case and the C to the power supply and E to the Arduino GND pin next to the 5V pin. I have a few BC547 PNP transistors that I scraped of old electronics. This should turn on the motor when setting pin 2 to high right?
iPot:
So I should connect the B to pin 2 in my case and the C to the power supply and E to the Arduino GND pin next to the 5V pin. I have a few BC547 PNP transistors that I scraped of old electronics. This should turn on the motor when setting pin 2 to high right?
Two changes:
Use an NPN transistor, not PNP.
Since the Base/Emitter junction acts like a diode to Ground you should add a resistor between Pin 2 and Base. About 100-200 Ohms.
EDIT: I attached a drawing of what I tried. The motor doesn't move at all when setting pin to HIGH. What side does the gold stripe on the resistor has to face (I think this is important).
EDIT2: i attached a different battery as I think the other 2 are empty and the motor starts spinning without even setting pin 2 too high. I must have done something wrong.
EDIT3: I switched the E and the C I guess. Trying it out.
EDIT4: Motor does't spin but also doesn't when setting pin2 to high.
What side does the gold stripe on the resistor has to face (I think this is important).
It doesn't matter - it is not at all important (unless you have many resistors on a board, in which case it is best to orientate them all the same way)
What side does the gold stripe on the resistor has to face (I think this is important).
It doesn't matter - it is not at all important (unless you have many resistors on a board, in which case it is best to orientate them all the same way)
Please draw a sketch of your wiring.
I did draw a sketch of my wiring in the post above.
I am just soldering some wires together (I need some practice anyhow). I just finished the circuit. When putting the resistor in pin 2 and the split end in the GND of the Arduino (while its off), the motor sttarts working. When sending my signal it goes a bit faster for a second.
It should be OFF from the start and go ON for a second when sending my signal. Looks like something is messed up.