I am trying to digitally control the speed of a motor with two buttons; one to increase its speed, and one to decrease.
I wired up the buttons first (using 10K pull-down resistors) and used the serial output to confirm that they were behaving as desired. Pressing the ‘up’ button incremented and printed out the current speed selection, up to a maximum of 10, and the down button worked similarly.
When I connect up the motor however, whilst the speed control seems to work, I seem to get random button press events which cause the motor to slow back down again. I can see they are registering as button presses as the button press code is firing and printing to the serial out. I can’t see what could be causing this in the code and so I am assuming it is to do with my circuit, perhaps a current issue. I was originally powering the circuit from my laptop’s USB port but I have switched to a bench PSU and this has not helped. My electronics knowledge is rudimentary, and my components store is limited to what comes in the kit (although I had assumed the components supplied would play nicely with each other?)
Any suggestions as to what I miht be doing wrong would be greatly appreciated. Thanks.
Here is my circuit http://www.freeimagehosting.net/4l9o8
Here is my sketch:
const int upButton = 7;
const int downButton = 8;
const int motorOut = 12;
//button press vars
int on = 1;
boolean press = false;
I have seen examples using analogWrite to vary the voltage supplied to a motor, but I have heard that whilst that works for an ideal motor, in real life a motor's speed does not vary linearly with its voltage and so it is better to use a pulsing approach, where the motor is either fully on or fully off at varying ratios. Should a digital output not be suitable for this (with an appropriate resistor)?
The switches could do with debouncing, true, but the random button presses occur when I'm not even touching the switches, so it's not a bounce problem. The 'press' boolean prevents a single press from being registered multiple times; it forces the arduino to wait until the button has been released before registering another press. The lack of debouncing does sometimes cause a single press to be registered twice, but the problems I'm talking about occur outside of that issue.
Hi,
Looking at your circuit, your resistors appear to be in the wrong place. Look up 'Pull Up Resistor' for more info
You should also look up decoupling capacitors, with a motor you should at least have 0.1 uf decoupling capacitor although its common to use three. You should also have a larger one to fill in for the power supply when the motor starts.
Oh, that's a mistake in the diagram; they are wired correctly so as to be in-circuit when the switches are open. I have tried adding a couple of decoupling capacitors across the rails but it didn't seem to help. The best I have here is a 100uF and a 220uF. I tried putting both in.
Heres a quick and easy test, take out the capacitors, replace the motor with an LED and current limiting resistor. My guess is that you will not see phantom inputs. If you put the motor back in, I assume you will see phantom inputs as a result of electrical noise coming from the motor.
If you have any scrap electronics in the house it will almost certainly have a few .1uf capacitors inside which should help to surpress the motor noise.
The LED did not show any signs of phantom inputs as you predicted. The best I could find is a 1uF capacitor which I have put across the motor. It seems to have helped a bit so I think if I get one with a more appropriate value and maybe put two or three of them in then my problems should be over.