# 12.1 DC Motor

The DC gear motor to be used is of 3\~5V, which looks as:

![DC Motor](/files/-LCFETtjJxZrPWxcJMO9)

In order to estimate the race car's speed, we also provide a speed encoder for each DC motor.

![DC Motor + Speed Encoder](/files/-LCFETtz6f1zwYJo2UhM)

Clearly, two welding spots come with the DC motor, which we need to weld out the electrical wires manually.

![DC Motor with Soldered Wire](/files/-LCFETuCQ5TeLjq9PLnG)

## Hardware Wiring

![DC Motor](/files/-LCFETuek6zX-Bd--zYx)

## Sketch

The code can be found at [Examples\_Arduino - adafruit - \_001\_DCMotor - \_001\_DCMotor.ino](https://github.com/LongerVisionRobot/Examples_Arduino/blob/master/adafruit/_001_DCMotor/_001_DCMotor.ino) You can also refer to Adafruit's official website at <https://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors/overview>.

```
/*
/*
Adafruit Arduino - Lesson 13. DC Motor
*/

int motorPin = 3;

void setup() 
{ 
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  while (! Serial);
  Serial.println("Speed 0 to 255");
} 

void loop() 
{ 
  if (Serial.available())
  {
    int speed = Serial.parseInt();
    if (speed >= 0 && speed <= 255)
    {
      analogWrite(motorPin, speed);
    }
  }
}
```

## Note

We will use 4 **L298N** DC motors in our race car. Please refer to [**Chapter 15.1 - 4-Wheel Driven Using 4 DC Motors**](/arduino-full-stack/15_assembling/01_4wheel_dcmotor_driven.md) for more details.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://longer-vision-robot.gitbook.io/arduino-full-stack/12_motors/01_dcmotor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
