How to interface a DC motor with Arduino UNO using TinkerCAD?

Updated: Mar 9

The speed of the DC motor, in general, is directly proportional to the supply voltage, thus if the supply voltage (let's say V) is reduced to half of the initial value, then the speed of the DC motor will also become half of the initial value of speed. But practically it is not feasible to change the supply voltage every single time in order to control the speed of the DC motor. Speed of the DC motor can be regulated using Pulse Width Modulation (PWM) by #interfacing the DC motor with #Arduino UNO.


Duty Cycle is defined as the ratio of the digital 'high' period to the total sample period.

Duty Cycle = Ton/(Ton+Toff)


In this article, we will interface a DC motor with an Arduino UNO using TinkerCAD #simulation software. Simulating a circuit before implementing the circuit manually helps to visualize the connections and it also provides the output under different conditions and constraints which would help to cross-check the output of the circuit implemented manually.


A list of components required to implement the circuit both manually and using TinkerCAD are given below.

Hardware Requirements:


1.Breadboard :


Breadboard

A #breadboard is a flat piece of wood that is provided with holes that allow users to build a circuit including the components and connections without the need for soldering due to which it is reusable and finds its best usage for the students who are new to build circuits. The holes in any horizontal line will be in series whereas the vertical holes remain at an equipotential state. There are different sizes of breadboards available in the market which include "full size", "half-size" and, "mini size" breadboards.

You can purchase breadboard here.


2. Arduino UNO:


The Arduino is a programmable #microcontroller, it's an open-source electronics proto-type platform. It interacts with the user by receiving input with the aid of sensors and provides the output employing LED, buzzer, etc.,

Arduino UNO

In order to know more about the Arduino UNO, go through this article.

You can purchase Arduino UNO here.


3. DC Motor:

It is a type of electrical machine that converts electrical energy into mechanical energy. It takes electrical power through direct current and converts this energy to mechanical rotation.

You can purchase dc motor here.


4. Jumping Wires: Jumper wires are used to establish connections between different components in the circuit.


Jumping wires

You can purchase Jumping wires here.


5. Resistor: Resistor as the name signifies, opposes the flow of electrons. The Resistor is a passive element and has two terminals. They are basically used to monitor the current flow and also as voltage dividers. The #resistor that we would use in this circuit is a 220-ohm resistor and the color code for this resistor is Red, Red, Brown & Gold.


Resistor

You can purchase resistors here.


6. Transistor: The Transistor is a semiconductor device that is basically used to amplify or switch electronic signals. It consists of three layers of semiconductor material and it has three terminals namely Base, Emitter, and Collector. #NPN transistor is used in this circuit.


Transistor

You can purchase transistors here.


7. Diode: #Diode is a two-terminal electronic device that primarily conducts current in one direction (i.e it will have negligible resistance in one direction and very high resistance in the other direction). It contains two layers of semiconductor material.


pn junction diode

You can purchase diodes here.


Software Requirements :


TinkerCAD Software:


TinkerCAD

It is an easy-to-use online simulation software that is equipped with all the essential components which are required to build the circuit and analyze it.


Visit the TinkerCAD website.


Circuit Connection :


Circuit diagram-interfacing DC motor with Arduino

Circuit connection involves the following steps :

  • Initially, PIN number 3 of the Arduino UNO is used to supply the PWM signal to the base of the NPN transistor via a 220-ohm resistor.

  • The Collector of the transistor is connected to terminal 1 of the DC motor.

  • The Emitter of the transistor is connected to the ground.

  • Terminal 1 of the DC motor is also connected to the diode which is further connected to the 5V supply.

  • Terminal 2 of the DC motor is directly connected to the 5V supply.

  • Power to the circuit will be provided by the Arduino UNO.

Code Description:


Let us learn the code to control the speed of the DC motor.

const int Motor_pin = 3;

The Motor pin is set as 3 since we have used the 3rd pin of the PWM signal as the input to the DC motor.

int flag,Speed;

We initialize two integer type variables viz., flag and speed.

void setup()
{
 Serial.begin(9600);
 pinMode(Motor_pin, OUTPUT);
 Serial.println("Enter a value from 50 to 225"); // Why not from one ?, 
 Because motor will not get enough current below 50 so the motor will not 
 move.  
}

void setup is a function and in this, we initiated the serial communication at the baud rate of 9600. Now, the motor pin is defined as the output, and we print the statement Enter value from 50 to 225. If the PWM value is less than 50 then the motor will not rotate due to less current.

void loop() 
{
  if (Serial.available()>0)                   
  {
    Speed=Serial.parseInt();                 
    Serial Buffer.
    flag=0;
  }

Here, we check if the data is available in the serial port or not. If it is available then the data will be parsed into the integer format and will be stored in the speed variable. The Flag variable will be set to zero.

if (Speed>=50 && Speed<=255)                
  {
    analogWrite(Motor_pin,Speed);           
    if (flag==0)
    {
      Serial.print("Current speed is ");
      Serial.print(Speed);
      Serial.println(" PWM");
      flag=1;
    }
  }
}

If the speed variable carries a value that lies between 50 and 255 then the next line of the code will be executed. Here analogWrite function is an output type function that will give output to the Motor_pin i.e., PIN number 3. It will generate a PWM signal with a duty cycle that we have given to the speed variable. Now, we check the condition-if flag variable is zero or not. If it is zero, then we will print the speed of the motor and set back the flag variable to zero.


Complete Code:

const int Motor_pin = 3;
int flag,Speed;

void setup()
{
 Serial.begin(9600);
 pinMode(Motor_pin, OUTPUT);
 Serial.println("Enter value from 50 to 225"); // Why not from one ?, Because motor will not get enough current below 50 so motor will not move.  
}

void loop() 
{
  if (Serial.available()>0)                   // Checking if data is present in serial buffer or not.
  {
    Speed=Serial.parseInt();                  // Reading data from Serial Buffer.
    flag=0;
  }

  if (Speed>=50 && Speed<=255)                
  {
    analogWrite(Motor_pin,Speed);             // Sending the PWM signal to arduino pin 3.

    if (flag==0)
    {
      Serial.print("Current speed is ");
      Serial.print(Speed);
      Serial.println(" PWM");
      flag=1;
    }
  }
}


Download the complete code :

dc_motor_interface_code
.txt
Download TXT • 795B

NOTE: To simulate the circuit in #TinkerCAD the default code present in the window needs to be removed and copy-paste the downloaded code.


Working :


To start with first visit the TinkerCAD website. Create an account and move to the circuits section. By clicking on create a new circuit, you will be driven to a window where you can build the circuit. It will be equipped with all the components required and you can just drag the elements necessary and connections can be made using jumper wires.


To know more about TinkerCAD, you can just surf through the website or you can watch the video.


Upload the code in the code section and start simulating to check the output of your circuit.


The image below shows the output for a random input value lying in the range between 50-255 :

image showing the output

By changing the input value given to the DC motor, the duty cycle and hence the speed of the DC motor can be regulated.


Watch the video attached below for a clear understanding of how the connections are made and the simulation procedure.


Video By - Harshit Gupta

Simulation software plays a leading role in designing huge circuits which help to understand, analyze and provide outputs for a large set of input values which would be hectic doing it manually. It helps in avoiding any damage to the hardware by going beyond the rated value.


Validate your new circuit ideas and know the scope and boundaries of your circuit.


See Also:

Building a Digital Voltmeter using Arduino UNO.

Interfacing a 7-Segment Display with Arduino

Light Intensity Measurement using LDR sensor and Arduino on TinkerCAD


  • Facebook
  • YouTube
  • Instagram
  • LinkedIn
LearnElectronics
Contact Us