so thanks to some of the great users on this forum I got some code to start working with my motor. Just some background, its a 6rpm 3-12v motor that I am running with a 9v external power supply. I hooked it up to a transistor and plugged in the battery and everything was great. So I decided to control it with a push button. the button is connected through a 2K ohm resister into the 5v internal power, and the other lead connected to the gnd and to pwm 10. Just to test the button I used the following code
// Define which pin to be used to communicate with Base pin of TIP120 transistor
boolean currentlyOn = false;
int TIP120pin = 11; //for this project, I pick Arduino's PMW pin 11
void setup()
{
}
void loop() {
boolean on = (digitalRead(10) == LOW);
if (!on && currentlyOn) {
analogWrite(11, 0); // turn off
currentlyOn = false;
}
else if (on && !currentlyOn) {
analogWrite(11, 255); // turn on
currentlyOn = true;
}
}
the int tip120pin is just there from the past code I was using to just get the motor to listen to pwm 11 to control the speed, but mainly just to incorporate the arduino into the circuit. now for the problem.
if the digitalread on pwm 10 is set to low, as soon as I supply power the motor stops and pressing the button does nothing. if I change the pwm 10 to high, then the motor doesn't run when I supply power and pressing the button does not work.
for the record that im not proud of. I know that the button works because during one of the rewires I forgot the resistor and pressing it shorted the board, luckily no damage was done, and it just rebooted it.
the button is connected through a 2K ohm resister into the 5v internal power, and the other lead connected to the gnd and to pwm 10.
I need to see a picture of that.
It's far simpler to wire one leg of the switch to ground and the other leg to a digital pin. Then, turn the internal pullup resistor on, either by writing the INPUT pin HIGH or using the INPUT_PULLUP mode.
It's far simpler to wire one leg of the switch to ground and the other leg to a digital pin. Then, turn the internal pullup resistor on, either by writing the INPUT pin HIGH or using the INPUT_PULLUP mode.
Its literally a jumper attached to a button lead, then held against the resistor with an alligator clamp and then the resistor plugged into the 5v. Do you think that is the issue instead of something in the code?
oh yes thanks for reminding me, that's why I had created the into,
boolean currentlyOn = false;
int TIP120pin = 11; //for this project, I pick Arduino's PMW pin 11
void setup()
{
pinMode(TIP120pin, OUTPUT); // Set pin for output to control TIP120 Base pin
}
void loop() {
boolean on = (digitalRead(10) == LOW);
if (!on && currentlyOn) {
analogWrite(11, 0); // turn off
currentlyOn = false;
}
else if (on && !currentlyOn) {
analogWrite(11, 255); // turn on
currentlyOn = true;
}
}
But even with this I get the same error, as soon as I attach power it starts spinning and when I change low to high it doesn't spin and in both scenarios the button does nothing, I'm really starting to think I miswired...
resistorswtf:
Its literally a jumper attached to a button lead, then held against the resistor with an alligator clamp and then the resistor plugged into the 5v.
It is much too easy to misunderstand verbal descriptions of wiring and waste hours talking at cross-purposes. Make a drawing of the circuit (not a photo of the hardware) and post a photo of the drawing.
Robin2:
It is much too easy to misunderstand verbal descriptions of wiring and waste hours talking at cross-purposes. Make a drawing of the circuit (not a photo of the hardware) and post a photo of the drawing.
...R
Agreed, attached is my schematic, ignore the diode across the transistor as that has been pointed out as being useless. I didn't have time to work on it before school this morning due to my laptop wanting to update, but I think I may have found the issue. I used another users code for the Boolean, but I don't think, since I have not gone the internal pull up resistor route, that I have set pwm 10 to be an input. So I am pressing the button, its is completing the circuit, but the pin doesn't know that its supposed to be receiving any instructions. As Paul and Larry said, I will get home and put in a
pinMode(10,INPUT_PULLUP);
and I don't doubt that should solve the issue. But just in case, attached is the wireing.
The ground for the powersupply/transistor is the GND by the PWM's but I think a GND is a GND and should matter if its plugged into that one, or the one plugged in on the analog side.
That switch is NOT wired correctly. Get rid of the resistor. Connect one leg to ground. Connect the other leg to the digital pin. Use the internal pullup resistor.
PaulS:
That switch is NOT wired correctly. Get rid of the resistor. Connect one leg to ground. Connect the other leg to the digital pin. Use the internal pullup resistor.
I agree, when I was reading the arduino pushbutton tutorials here on the site, it said "We connect three wires to the Arduino board. The first goes from one leg of the pushbutton through a pull-up resistor (here 2.2 KOhms) to the 5 volt supply. The second goes from the corresponding leg of the pushbutton to ground. The third connects to a digital i/o pin (here pin 7) which reads the button's state."
I thought the wording was a little weird, but tried to replicate it on mine. If I did it the way that you are saying, with one leg to groud and other to pin, is the pin providing the power to the button? I think that's where I wasn't thinking correctly. Instead of the button just being a switch to pass the current, I was reading that it needed a power supply...that seems silly now that im saying it out loud.
It would also explain why the board shorted that one time, not just because I forgot the resistor, but also because I was literally just passing a 5v power through a resistor and into the ground.
If I did it the way that you are saying, with one leg to groud and other to pin, is the pin providing the power to the button?
Yes. The internal resistor pulls the pin up (to 5V). That's why it's called a pullup resistor. Pressing the switch shorts the pin to ground. Since the short circuit is much lower resistance, the pin will read LOW when the switch is pressed. It will read HIGH, because the resistor pulls the pin HIGH, when the switch is not pressed.
The schematic on this page: http://www.arduino.cc/en/Tutorial/DigitalReadSerial shows how to wire a switch with an external resistor. The resistor goes between the pin and ground, not between the pin and 5V.
You have pin D10 connected to the wrong leg of the pushbutton. As shown, one leg is pulled high; and one leg is connected to Gnd and to D10, so D10 is always low. When pressed, the pulled up leg gets connected to Gnd. D10 never goes high.
If you're going to continue with projects, get some schematic capture software. Fritzing pictures are not the best for showing your design. www.expresspcb.com has free schematic software that is easy to use.
PaulS:
The schematic on this page: http://www.arduino.cc/en/Tutorial/DigitalReadSerial shows how to wire a switch with an external resistor. The resistor goes between the pin and ground, not between the pin and 5V.
CrossRoads:
You have pin D10 connected to the wrong leg of the pushbutton. As shown, one leg is pulled high; and one leg is connected to Gnd and to D10, so D10 is always low. When pressed, the pulled up leg gets connected to Gnd. D10 never goes high.
If you're going to continue with projects, get some schematic capture software. Fritzing pictures are not the best for showing your design. www.expresspcb.com has free schematic software that is easy to use.
I will get that software for when I am at home, the school comps don't give us admin rights to download stuff.
CrossRoads:
Yes, but only you & I could see that apparently.
This line:
boolean on = (digitalRead(10) == LOW);
Is that legitimate syntax? I would have expected just this:
boolean on = digitalRead(10);
The syntax was questionable to me as well, but when I compiled and uploaded it I received no errors, so I was taking a "if it isn't broke" approach.
yaafm:
If you assume (say) that digitalRead(10) returns LOW then the first will set the boolean variable on to true.
The second will set the boolean variable on to LOW. The first is therefore more correct as true is boolean but LOW is not.
Similarly if you assume (say) that digitalRead(10) returns HIGH then the first will set the boolean variable on to false.
The second will set the boolean variable on to HIGH. The first is therefore more correct as true is boolean but HIGH is not.
OK so ultimately depending on what you consider as "on" in the rest of the logic they may both work - but that's not really the point.
Oh ok, it took me a couple of reads to understand that. I believe that you were referencing the post before yours about setting "on" to not only reference the pin, but also setting it to a LOW/HIGH value by default. So if I kept it my way, if it was low, pressing the button wouldn't do anything because it would just change it from low to low, and vice versa for high. I think that's what you were trying to get through to me, I am not the brightest, so if you were referring to something else, I am sorry.