So I recently started a project and I need to make a circuit where I press a pushbutton and it makes a DC motor turn at a certain speed for a specific amount of time. I've tried to do it a couple times, but I can't seem to figure it out (I'm new to Arduino code ). Does anyone have some code for it or at least a starting point for me?
You start by reading the "how to use the forum-please read" stickies so that you know how to post code and errors and have some idea of the information that we need to be able to help you.
Then you draw a schematic of the project that shows all of the parts, the part numbers and/or values including all power supplies. Post an image of the circuit like this. Also post your code with a description of what it is supposed to do and what it actually does that is not what you want. If the code will not compile post the full text of the error messages.
This Simple Image Guide has the essentials.
And post the program that represents your best attempt.
...R
Get the push button working.
Look at methods to control the speed of your motor, and the hardware you might need.Write some code for that.
Look at how you might generate time delays , write some code for that to turn an led on/off .
Have a look at the various examples in the file /examples folder, check out the playground on this site.
Sorry about my formatting, first post. I still should've read the stickies though, so thanks for that. My best attempt is this:
int motorPin = 13;
int inPin = 7;
int val = 0;
void setup() {
 pinMode(motorPin, OUTPUT);
 pinMode(inPin, INPUT);
}
void loop(){
 val = digitalRead(inPin);
 if (val == HIGH) {
  digitalWrite(motorPin, LOW);Â
 } else {
  analogWrite(motorPin, 150);
  delay(250);
 Â
  analogWrite(motorPin, 0);
 }
}
Here is an image:
I'm using an Arduino Uno and plugging it into my computer and a DC power adapter. My motor is 12 volt, and I don't know anything other than that about parts. The motor works when it is plugged into ground and 5V directly.
TrentU:
My best attempt is this:
What happens when you run that program and what do you want it to do that is different?
Being able to describe the symptoms and the requirement is a large part of successful programming.
Also, what do you think may be the problem?
Having a hypothesis provides a basis for further testing.
...R
My goal with this is to have the motor turn for a specific time at a specific speed.
There are two main problems here. First, when I run the program, it doesn't work (irl). Second, when I run it on a simulator, the motor turns for a specified amount of time, but not a specified speed.
The motor won't turn because an Uno output pin cannot supply enough current to drive a motor. If you are lucky you did not blow the pin. A driver (transistor or motor driver) is required to supply the current under control of a digital (PWM) output.
The choice of a proper transistor or motor driver depends on the stall current of the motor and whether the motor turns in only one direction or must be reversible.
Thanks for the help. I'm a complete newbie, so I don't really know how to add a transistor to this (I do have some though). Also, is there a way to check for a blown pin?
Load the blink example (in the IDE, File, Example, Digital, BlinkWithoutDelay). Then connect a 330 to 470 Ohm resistor and a LED from pin 13 to ground and run the BlinkWithoutDelay example. I the LEDs blink, the pin is probably OK. Check any output pin the same manner but change const int ledPin = X (in the sketch) to the pin under test.
A MOSFET transistor motor driver. The exact MOSFET to use depends on the stall current of the motor.
UPDATE:
I made an assumption as to what the collector, base, and emitter do, and plugged in some stuff. Here is the updated version:
The code is there too. Now there is another problem: in real life, the circuit is inverted; the motor is always spinning and when I press the button the motor stops. The problem of the speed still exists as well.
[I made this post before the previous two]
I made an assumption as to what the collector, base, and emitter do, and plugged in some stuff.
Stop doing that. There is part number on the transistor. Google the part number + "datasheet". The data sheet will show the pin configuration. And "plugging in some stuff" is a great way to destroy stuff.
A NPN bipolar transistor motor driver. Note the diode is required to protect the transistor from inductive spikes. And again the right transistor depends on the motor stall current. A 2N222 is good for about 600mA (max continuous collector current is also found on the data sheet).