ArduinoM0 Pro DC Motor Example

DC Motor Example

This example shows how to control a DC Motor using an Arduino board and a Potentiometer.
The motor speed will depend on the potentiometer status.
The more you reduce the resistance in the potentiometer, the more the motor speed will be set, and viceversa.

Arduino boards are designed to let signals flow, so they can't handle high current values.
To power the DC Motor you will to connect it need an external power supply.
How can we limit the current flowing in the DC Motor?
We'll use a NPN transistor as a variable resistance to set the motor speed.
The Arduino board M0 Pro operates on 3.3V. The maximum voltage that the I/O pins can tolerate is 3.3V. The application of a voltage exceeding 3.3V may damage the board.

Hardware Required

  • Arduino Board
  • DC Motor
  • Transistor
  • Potentiometer
  • 1K Resistance
  • A bunch of wires


Connect the Potentiometer VCC to 3.3V and the ground to GND. The signal PIN goes to Analog Input A0 of your Arduino board.
The 9V coming from the battery needs to be separated from the 3.3V of your Arduino differently from the Ground, which needs to be in common.
The transistor used is a BJT npn.
Proceed connecting the transistor: connect the "Base" to a 1KOhm resistor and then to PIN 3; the "Collector"pin to the 9V of the battery and connect the "Emitter" directly into the motor.
The remaining PIN of the DC Motor needs to be connected to the ground.


The sketch starts by setting the PIN 3 (the one used to control the transistor) to output mode.
The loop function, each time, reads the value from the potentiometer PIN (D3).
The value is then mapped in order to set the motor speed, through the transistor.
Finally, it sets the Transistor PIN to the value mapped.
The motor speed is now changed.

void setup() { 
	//Transistor PIN
	pinMode(3, OUTPUT);
void loop() {
	///Read a value from PIN A0
	int potValue = analogRead(A0);
	//Map the value read from the potentiometer
	//to control the motor
	potValue = map(potValue, 0,1023,0,255);
	//Set the Motor Speed
	analogWrite(3, potValue);