Hardware Reference
In-Depth Information
const int POT=0; //POT on Analog Pin 0
int val = 0;
void setup()
{
pinMode (MOTOR, OUTPUT);
}
void loop()
{
val = analogRead(POT);
val = map(val, 0, 1023, 0, 255);
analogWrite(MOTOR, val);
}
A lot of this code should look familiar from your previous experience dealing
with analog sensors. Note that the
constrain
function is not required when
using a potentiometer, because you can use the entire input range, and the value
will never go below
0
or above
1023
. After uploading the code to your Arduino,
adjust the pot and observe the speed of the motor changing accordingly.
UsinganH-BridgetoControlDCMotorDirection
So, you can change DC motor speed. This is great for making wheels on an
Arduino-controlled robotā¦ as long as you only want it to drive forward. Any
useful DC motor needs to be able to spin in two directions. To accomplish this,
you can use a handy device called an
H-bridge
. The operation of an H-bridge
can best be explained with a diagram (see FigureĀ 4-5).
Figure 4-5:
H-bridge operation
Search WWH ::
Custom Search