Published on

Workshop Day 3 Session 1

Table of Contents

Introduction

Hello! Congratulations on making it to Day 3 of the workshop. In this session, you will be learning about the various sensors present in your kit and how they can be interfaced with the Arduino Uno.

Motors and Motor Drivers

About

In everyday life, we see rotating devices like a drill, wheels in a vehicle, and a fan that make our tasks easier because their ability to "turn" helps in these cases. But what is the cause that drives them to turn? Well, this cannot be the sole job of electrical or manual work but is a combined effort of certain laws that lead to an electric motor's principle.

Electric Motor

Energy transformation is the basic principle of an electric motor, and it is pretty evident that in this case, it is from electrical energy to the energy that leads to the motion of a rotor which indeed is mechanical. But what is the cause of the rotation? There has to be a force responsible for this turning effect known as torque. Here, the interrelation between electric current and magnetism helps. It is a known fact that a current-carrying conductor experiences a force in a magnetic field and when it is a conducting loop, it experiences a torque! So torque of a motor is indeed an important factor for selecting a motor for our purpose.

Components of an Electric Motor

The basic parts include:

  • Rotor
  • Stator
  • Brush
  • Commutator
  • Armature
IR

Working

IR

It is essential to understand that providing power to a motor depends on its size and complexity. A smaller motor can be manually supplied power through a battery, whereas a larger motor, the ones that are used in industries and other high power-driven motors, requires a mechanism which, when energized, connects its terminals to the high power supply. This method of starting a motor is called a "direct on line starter method."

Another fact about these motors is that they draw a huge amount of current to go to the full-speed state from the initial state (to overcome inertia). This leads to drawing a huge amount of starting current from the power supply (for higher starting voltage across the terminals), which may cause damage to the circuits. So "adjustable speed-drives" and "current limiting resistors" are used to prevent problems to the circuits and the motor itself.

Types of control in a system

Now that we got a good idea about motors let us discuss the two control systems: open-loop and closed-loop systems. The reason behind discussing these is to know about how control is established in various systems.

1) Open-loop system:

○ This is used when the system under consideration is not complex and need not require that much supervision for its working—for example, the lighting in your room, we just turn the lights on, but we do not ensure if it is working with the same brightness for every time interval. The same is the case with ordinary DC Motor; there is no special mechanism to ensure if it is maintaining a constant rpm or torque for every time interval. If it is fine, it works, else it undergoes certain problems.

IR

The above picture shows an open-loop system. ● Commanded variable: The output that you desire. ● Controller: Arduino or any other controller ● Actuator: The device which actually does the process or which enables other devices to work on the process ● Process: The task which we want to perform(here, rotation of the motor) ● Controlled variable: The Actual result/output the we get

2) Closed-loop system:

○ It is clear that in an open-loop schematic, there can be a deviation of the "controlled variable" from the "commanded variable" called the "error variable". This error can cause undesirable consequences when working on huge projects. Here comes the closed-loop system. The special feature of this is that there is a feedback mechanism with the help of sensors that lets the controller know about the situation of the controlled variable with which we can measure the error factor and make our output as desirable as possible.

IR

Other types of Motors

1) Servo Motor

  • This motor follows a closed-loop system. So it has got the advantage of precision. This motor provides high torque along with the precise position of the shaft, which gives its feedback control.

    IR
  • The above is the schematic of the servo motor. It is to be noted that input is given to it in the form of PWM signals (pulse-width modulation) which leads to the position control of the motor (Of Course, digital signals also can be used for its position control). Also the position sensors in this closed-loop control are generally potentiometers.

  • This is not all; servos can also be encoded depending on the complexity of the task that is involved making them gain access to speed and torque feedback along with regular position control. And here comes the actual problem of using these complex closed-loop systems when it is essential to perform the task but such skilled workers are not available. Skilled programmers are essential for optimizing the internal algorithms of closed-loop systems which is not an easy task.

So there is another type of motor that is in much use in the real world, the stepper motor.

2) Stepper Motor

This motor utilizes an open-loop system.

  • Stepper motors have a permanent magnetic rotating shaft called a rotor and stationary electromagnets surrounding the rotor called the stator.

    IR
  • Stepper motors have typically 50 to 100 electromagnet poles (pairs of north and south poles) generated either by a permanent magnet or an electric current.

  • The greater the number of poles the more is the precision.

There are two main ways in which a stepper motor can be driven:

Full-step mode:

The rotor turns through a bigger angle at once. This can be done by energizing either one or two phases( in general) at a time.

IR

It can be seen that the torque in two phase on mode is higher than that of one phase one mode.

Microstepping:

This mode is essential when precision is an important factor in the process. In this case, the rotor is controlled by two phases such that the current in both of them is varied such that the magnetic field produced keeps varying with time, and so is the torque acting on the rotor. This is done such that the rotor moves through small angles( depending in the magnitude of the current and the way you vary it).

IR

In general, Servo Motors run more smoothly than a stepper motor except when micro stepping are used.

A Servo Motor will typically provide 2-3 times the speed of a typical stepper motor as the Servo Motor speed increases, the torque of the Servo Motor remains constant, thus performing better than a stepper motor at higher speeds usually above 1000 RPM

H-Bridge

The direction in which the motor rotates is expected to change depending on the situation and this cannot be always manually changed. Here comes the role of a H-Bridge which enables the change in direction of the motor by changing the state of the switches in its circuit which can be accomplished by logical HIGH or LOW to the respective switch.

IR

It is evident from the above diagram about the way in which this works. When S1 and S4 are closed (S2 and S3 being open), the motor rotates in one direction while on closing S2 and S3 (keeping S1 and S4 open) the motor turns in the other direction.

IRIRIR

This above diagram shows shorting of circuit which may lead to the burning of H-Bridge.

Also it is not advisable to drive the motor and change the direction at the same time.

IR

Motor Driver

A motor driver at a basic level is an integrated circuit chip used to control a motor as per given instructions with the help of H Bridge topology in the case of the L293D.

Well, lets break this down and look into it closely one at a time.

Why do we need a motor driver?

  • In the world of autonomous technology, we would require efficient communication between microcontrollers like the arduino and a motor which draws a huge amount of current to work at the desired rate. Meanwhile, micro controllers operate on low level voltage/current (Eg: Arduino has an operating voltage of 5V).
  • Thus, to bridge this gap of power output from the arduino to the motor, a motor driver can act as the interface taking in the low current signals from the Arduino and converting them into higher current signals which can help drive the motor as required.
  • Thus, we can control the speed and direction of rotation of motor based on voltage provided.
IR

The L293D motor driver

We will now specifically look into the L293D

IR
  • It is a commonly used and a popular 16 pin motor driver IC.
  • It is capable of running 2 motors at the same time and works on the principle of a H Bridge.

Working principle

The IC works on the principle of H-Bridge (this can be understood on checking how the IC conveys signals to the motor).

IR
  • As we see, VCC2 is the pin that drives the motor at its required voltage and VCC1 is for charging the 16-pin IC which is connected to the Arduino 5V pin. One important point is that whenever there are any components in a project, we need to provide a common ground to them. Same is the case here, all the ground pins in the circuit are to be connected with the GND pin of the Arduino.
  • It should also be noted that the enable pins are going to control the speed of the rotation through the PWM signals (Pulse width modulation) which can be established by connecting them to the PWM digital pins on Arduino.

Reference

L293D PIN DESCRIPTION

The following image shows the pinout of the L293D.

IR

Understanding this is very crucial to facilitate its usage in the circuit.

Pin NumberPin NameDescription
1Enable 1,2This pin enables the input pin Input 1(2) and Input 2(7)
2Input 1Directly controls the Output 1 pin, Controlled by digital circuits
3Output 1Connected to one end of Motor 1
4GroundGround pins are connected to ground of circuit (0V)
5GroundGround pins are connected to ground of circuit (0V)
6Output 2Connected to another end of Motor 1
7Input 2Directly controls the Output 2 pin, Controlled by digital circuits
8Vcc2 (Vs)Connected to Voltage pin for running motors (4.5V to 36V)
9Enable 3,4This pin enables the input pin Input 3(10) and Input 4(15)
10Input 3Directly controls the Output 3 pin, Controlled by digital circuits
11Output 3Connected to one end of Motor 2
12GroundGround pins are connected to ground of circuit (0V)
13GroundGround pins are connected to ground of circuit (0V)
14Output 4Connected to another end of Motor 2
15Input 4Directly controls the Output 4 pin, Controlled by digital circuits
16Vcc1 (Vss)Connected to +5V to enable IC function

Few points to be noted:

  • There is a notch on the top of the IC, to guide us with proper connections.
  • Input pins: The ones to which we give signals
  • Output pins: The ones connected to the motors.
  • The enable pins are there to “enable” the output pins. If enable pins are set to logic HIGH then the output pins match up to the input pins.
  • If the enable pins are set to logic LOW then regardless of logic states of input pins, the output pins are always set to zero.

Reference

You can also take a look at the datasheet of L293D for more information.

Now that we have a fair idea of the the L293D, lets use it in a circuit to grasp the complete picture

Using L293D with motors and Arduino

Circuit on TinkerCAD

Code

int speedPinR = 5; // connect enable pin 1,2
int dirR1 = 4; // connect input pin 1 which controls output pin 1
int dirR2 = 2; // connect input pin 2 which controls output pin 2
int speedPinL = 6; // connect enable pin 3,4
int dirL1 = 7; // connect input pin 3 which controls output pin 3
int dirL2 = 12; // connect input pin 4 which controls output pin 4
const int mSpeedL = 79; // to maintain approx 5000rpm for left wheel
const int mSpeedR = 79; // to maintain approx 5000rpm for right wheel
int dt = 1000; // delay time

// Output pins 1,2 connected to right motor
// Output pins 3,4 connected to left motor

void setup()
{
  Serial.begin(9600);

// set pinMode for all pins
  pinMode(speedPinR, OUTPUT);
  pinMode(dirR1, OUTPUT);
  pinMode(dirR2, OUTPUT);
  pinMode(speedPinL, OUTPUT);
  pinMode(dirL1, OUTPUT);
  pinMode(dirL2, OUTPUT);
}

void loop()
{
  digitalWrite(dirR1, HIGH); // input pin 1 set to high
  digitalWrite(dirR2, LOW); // enable pin 1,2 set to low
  analogWrite(speedPinR,mSpeedR); // 5000rpm FOR RIGHT

  digitalWrite(dirL1, HIGH); // input pin 3 set to high
  digitalWrite(dirL2, LOW); // enable pin 3,4 set to low
  analogWrite(speedPinL,mSpeedL); // 5000rpm FOR LEFT

  delay(dt); // set delay time
 }

TinkerCAD Simulation

Refer to the above to get an idea on how to make the connections and how to work with the L293D.

Here is a very useful video by Paul MCWhorter. Do give it a watch! https://www.youtube.com/watch?v=fPLEncYrl4Q

IR

Code

int dirR1 = 11;
int dirR2 = 10;
int dirL1 = 9;
int dirL2 = 6;
int mSpeedL = 250;
int mSpeedL1 = 0;
int mSpeedR = 250;
int mSpeedR1 =0;
int dt=1000; // delay time

void setup()
{
  Serial.begin(9600);

// set pinMode for all pins
  pinMode(dirR1, OUTPUT);
  pinMode(dirR2, OUTPUT);
  pinMode(dirL1, OUTPUT);
  pinMode(dirL2, OUTPUT);
}

void loop()
{
  analogWrite(dirR1,mSpeedR);
  analogWrite(dirR2,mSpeedR2);
  analogWrite(dirL1,mSpeedL);
  analogWrite(dirL2,mSpeedL2);

  delay(dt); // set delay time
 }

Ultrasonic Sensor HC-SR04

About

The HC-SR04 ultrasonic sensor is used to calculate the distance of an obstacle in the path of the bot. It consists of four pins: VCC, trigger, echo & ground. It also includes a transmitter and receiver, which transmit and receive the US signals, respectively.

IR

Working Principle

The HC-SR04 sensor detects the travel time of a signal from transmission till it's received. The sensor sends out a ping, and an echo is heard. The duration of travel of the signal is then measured by the sensor. When the Trig pin of the sensor receives a pulse of HIGH for at least 10 microseconds, it will initiate the sensor, which will transmit out 8 cycles of ultrasonic burst and wait for the reflected ultrasonic burst. When the sensor detects US signals from the receiver, it will set the Echo pin to HIGH and delay for a period proportional to distance.

The sensor transmits 8 cycles of ultrasonic bursts since the receiver needs to hear enough cycles to reach its full output and set the Echo pin to HIGH.

Pins Description

  • VCC: This powers the sensor with +5V and is connected to the 5V supply of the Arduino.
  • Trigger: It is an Input pin. It is connected to any digital pin on the Arduino. On sending a HIGH signal to this pin, the transmitter sends out US signals until a LOW signal is sent.
  • Echo: It is an Output pin. It is connected to any digital pin on the Arduino. This pin goes HIGH for the duration equal to the time taken by the receiver to receive US signals after being transmitted.
  • Ground: This pin is connected to the ground of the Arduino.

The following image shows the connection of the terminals of the sensor to the Arduino:

IR

Reference You can also refer to the datasheet for more information.

Distance Calculation

As described earlier, the HC-SR04 sensor sends out ultrasonic signals and receives the echo, then measures the pingTimeTravel of the signal.

We know, Speed=Distance/TimeSpeed = Distance \hspace{0.1cm} / \hspace{0.1cm}Time

Sound waves travel at a speed of about 340m/s340 m/s at room temperature. Speed=340m/s\therefore Speed = 340 m/s

The time given by HC-SR04 is in microseconds. We need to divide the time by 2 since this is the time taken by the sound waves from the transmitter to the object back to the receiver.

IR

Thus, the formula becomes, Distance=340(pingTravelTime/2)Distance = 340 * (pingTravelTime / 2)

You can watch the following videos to understand better:

Code

IR
int trigPin = 12; // connect trig pin of HC-SR04
int echoPin = 11; // connect echo pin of HC-SR04
int pingTravelTime; // initialize variable

void setup() {
    pinMode(trigPin,OUTPUT); // trig pin set as OUTPUT
    pinMode(echoPin,INPUT); // echo pin set as INPUT
    Serial.begin(9600);
}

void loop() {
    digitalWrite(trigPin,LOW); // trig pin set to low
    delayMicroseconds(10); // delay time in microseconds

    digitalWrite(trigPin,HIGH); // trig pin set to high
    delayMicroseconds(10); // delay time in microseconds
    digitalWrite(trigPin,LOW); // trig pin set to low

    pingTravelTime = pulseIn(echoPin,HIGH); // pulseIn() function explained below
    delay(25); // delay time
    Serial.println(pingTravelTime); // print pingTravelTime on Serial Monitor
}

pulseIn() Function

The pulseIn() function measures the time period of either HIGH or LOW pulse input signal. The syntax of pulseIn() function is: pulseIn(pin, value)

  • pin: the number of the pin on which the pulse is to be read.
  • value: the value of the pulse, either HIGH or LOW.

In the above code, the pin is the echoPin, and the value is HIGH, i.e. the function measures the time period of the HIGH pulse input signal on the echoPin.

Hence it indirectly measures the travel time of the signal. Thus the pingTimeTravel can be calculated using the pulseIn() function.

The HC-05 Bluetooth 2.0 Module

Ever wanted to use your mobile to control how a car moves? To do that, you need the HC-05 Bluetooth module. Here, we will understand the basics of Serial communication using HC-05 and a brief description of its hardware.

An Overview

Designed to replace cable connections, HC-05 uses serial communication to communicate with the electronics. It uses the 2.45GHz frequency band. The data transfer rate can vary up to 1Mbps and is in the range of 10 meters. The HC-05 module can be operated within 4-6V of power supply. It supports baud rate of 9600, 19200, 38400, 57600, etc. We use this module because Arduino can't interface using Bluetooth.

Pin Diagram

IR

The HC-05 has five pins, namely:

  1. Key: This pin is used to set the Data Mode or AT command mode (set high).

    • Data mode: Exchange of data between devices. Baud rate is 9600bps in data mode.
    • Command mode: It uses AT commands which are used to change the setting of HC-05. Baud rate is 38400bps in command mode. An AT command is a string, e.g., +PSWD:1234 sets the password for communication to 1234.
  2. VCC: This is connected to a +5V power supply.

  3. Ground - Connected to the ground of the powering system.

  4. Tx (Transmitter) - This pin transmits the received data Serially. It would be best to connect this pin to the RX pin of the microcontroller you are using.

  5. Rx (Receiver) - Used for broadcasting data serially over Bluetooth. Connect this pin to the TX pin of the micro-controller.

  6. State - Used to check if the Bluetooth is working correctly. (Debugging)

How to connect the module to Arduino?

IR
  1. Connect Power Supply (based on the datasheet of modules) for Bluetooth and Microcontroller, which you are using.
  2. Connect TXD pin of HC-05 Bluetooth module to RXD pin of the microcontroller.
  3. Connect the RXD pin of the HC-05 Bluetooth module to the TXD pin of the microcontroller.
  4. Common grounding is needed for both modules.
  5. To prevent the module from damaging and make it work properly, you should use a resistance division circuit (5v to 3.3v ) between the Arduino TX pin and the module RX pin.

Sample Code for Arduino (for reference only)

To communicate with HC05 using Bluetooth, you need a Bluetooth terminal application on your phone. You can use this one. After opening the app, look for the HC-05 module and pair the device. After pairing, click on it to connect. To start transferring data, upload this code on your Arduino and connect HC05 using the app you have just installed.

#include <SoftwareSerial.h> //Necessary for bluetooth serial communication
SoftwareSerial MyBlue(2, 3); // RX | TX
int flag = 0;
int LED = 8;
void setup()
{
 Serial.begin(9600);
 MyBlue.begin(9600); //Communication Mode
 pinMode(LED, OUTPUT);
 Serial.println("Ready to connect\nDefualt password is 1234 or 000");
}
void loop()
{
 if (MyBlue.available()) //Checks if the bluetooth connection is online
   flag = MyBlue.read(); //Reads the value passed by the master device
 if (flag == 1)
 {
   digitalWrite(LED, HIGH);
   Serial.println("LED On"); //Turns LED on if 1 is passed
 }
 else if (flag == 0)
 {
   digitalWrite(LED, LOW); //Turns LED off if 0 is passed
   Serial.println("LED Off");
 }
}

To understand this better, try to draw an analogy between this and the Serial class used for communication between your PC and the microcontroller.

Resources

  1. Follow this tutorial to know how to use the Bluetooth app mentioned above.
  2. If you want to make your own app without in-depth knowledge in development, you can use MIT App Inventor. Follow this tutorial to get an idea of the basics.

LM7805 Voltage Regulator

About

Voltage regulators are used to ensure steady and constant output from voltage sources. The integrated circuits used for voltage regulation are called Voltage regulator ICs.

IC 7805 is a three-terminal Voltage Regulator that restricts the output voltage to 5V output for various ranges of input voltage. It acts as an excellent component against input voltage fluctuations for circuits, and adds an additional safety to your circuitry. Primary job of a voltage regulator is to drop a larger voltage to a smaller one and keep it stable, since that regulated voltage is being used to power sensitive electronics.

LM78xx series is a family of linear voltage regulators. This series of regulators are excellent for most purposes, they can handle up to almost 30V on the input and depending on the package, up to 1A output current. The last digit on the IC signifies the voltage to which input is regulated and given out, for example, 7805 regulates to give 5V output and a 7803 gives a 3V output.

IR

Pins Description

IR
Pin No.PinFunctionDescription
1INPUTInput Voltage (7V-35V)In this pin of the IC positive unregulated voltage is given in regulation
2GROUNDGround(0V)In this pin where the ground is given. This pin is neutral for equally the input and output
3OUTPUTregulated output; 5V(4.8V-5.2V)The output of the regulated 5V is taken out at this pin of the IC regulator

You can read more about the 7805 IC here. A detailed video explanation on the 7805 IC can be found here.

IR

Tinkercad Simulation for Reference How to use LM7805 regulator in 5 volt dc power supply from 9 volt battery

7805 Regulator Features

  • 5V positive voltage regulator.
  • Operating current is 5mA.
  • Minimum input voltage is 7V.
  • Maximum input voltage is 35V.
  • Required very minimum external component to fully function.
  • It can deliver up to 1.5A of current.
  • Internal thermal overload and short circuit current limiting protection is available.
  • Junction temperature maximum of 125-degree celsius.
  • This IC has both internal current limiting and thermal shutdown features.

You can also refer to the datasheet for more information.

Application of LM7805

Used as

  • fixed output regulator.
  • regulated dual supply.
  • In most devices as constant +5V output is needed for microcontrollers and sensors in most of the projects.
  • current limiter for certain applications.
  • polarity reversal protection circuit.
  • Reverse bias protection circuit.
  • positive regulator in negative configurations.