7

Overview

In this lesson, you will learn how to control a small DC motor using an Arduino and a transistor.
learn_arduino_overview.jpg
To build the project described in this lesson, you will need the following parts.

Part

Qty

learn_arduino_dc_motor.jpg
Small 6V DC Motor
1
learn_arduino_transistor.jpg
PN2222 Transistor
1
learn_arduino_diode.jpg
1N4001 diode
1
learn_arduino_R-270-level.jpg
270 Ω Resistor (red, purple, brown stripes)
1
learn_arduino_breadboard_half_web.jpg
Half-size Breadboard
1
learn_arduino_uno_r3_web.jpg
Arduino Uno R3
1
learn_arduino_jumpers_web.jpg
Jumper wire pack

Breadboard Layout

When you put together the breadboard, there are two things to look out for.
Firstly, make sure that the transistor is the right way around. The flat side of the transistor should be on the right-hand side of the breadboard.
Secondly the striped end of the diode should be towards the +5V power line - see the image below!
The motor that comes with Adafruit Arduino kits does not draw more than 250mA but if you have a different motor, it could easily draw 1000mA, more than a USB port can handle! If you aren't sure of a motor's current draw, power the Arduino from a wall adapter, not just USB
learn_arduino_breadboard.jpg
The motor can be connected either way around.

Arduino Code

Load up the following sketch onto your Arduino.  
  1. /*
  2. Adafruit Arduino - Lesson 13. DC Motor
  3. */
  4.  
  5.  
  6. int motorPin = 3;
  7. void setup()
  8. {
  9. pinMode(motorPin, OUTPUT);
  10. Serial.begin(9600);
  11. while (! Serial);
  12. Serial.println("Speed 0 to 255");
  13. }
  14. void loop()
  15. {
  16. if (Serial.available())
  17. {
  18. int speed = Serial.parseInt();
  19. if (speed >= 0 && speed <= 255)
  20. {
  21. analogWrite(motorPin, speed);
  22. }
  23. }
  24. }
The transistor acts like a switch, controlling the power to the motor, Arduino pin 3 is used to turn the transistor on and off and is given the name 'motorPin' in the sketch.
When the sketch starts, it prompts you, to remind you that to control the speed of the motor you need to enter a value between 0 and 255 in the Serial Monitor.

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

Flowchart

12เครื่องวัดความเร็วอะนาล็อกใช้ Arduino และเซ็นเซอร์ IR

10อัลตราโซนิกเซนเซอร์ตรวจจับระยะไกลใน ARDUINO พร้อม TINKERCAD