Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Programmable DC Motor Controller with an LCD

Save for later
  • 23 min read
  • 23 Jul 2015

article-image

In this article by Don Wilcher author of the book Arduino Electronics Blueprints, we will see how a programmable logic controller (PLC) is used to operate various electronic and electromechanical devices that are wired to I/O wiring modules. The PLC receives signals from sensors, transducers, and electromechanical switches that are wired to its input wiring module and processes the electrical data by using a microcontroller. The embedded software that is stored in the microcontroller's memory can control external devices, such as electromechanical relays, motors (the AC and DC types), solenoids, and visual displays that are wired to its output wiring module. The PLC programmer programs the industrial computer by using a special programming language known as ladder logic. The PLC ladder logic is a graphical programming language that uses computer instruction symbols for automation and controls to operate robots, industrial machines, and conveyor systems.

The PLC, along with the ladder logic software, is very expensive. However, with its off-the-shelf electronic components, Arduino can be used as an alternate mini industrial controller for Maker type robotics and machine control projects. In this article, we will see how Arduino can operate as a mini PLC that is capable of controlling a small electric DC motor with a simple two-step programming procedure. Details regarding how one can interface a transistor DC motor with a discrete digital logic circuit to an Arduino and write the control cursor selection code will be provided as well. This article will also provide the build instructions for a programmable motor controller. The LCD will provide the programming directions that are needed to operate an electric motor. The parts that are required to build a programmable motor controller are shown in the next section.

Parts list

The following list comprises the parts that are required to build the programmable motor controller:

  • Arduino Uno: one unit
  • 1 kilo ohm resistor (brown, black, red, gold): three units
  • A 10-ohm resistor (brown, black, black, gold): one unit
  • A 10-kilo ohm resistor (brown, black, orange, gold): one unit
  • A 100-ohm resistor (brown, black, brown, gold): one unit
  • A 0.01 µF capacitor: one unit
  • An LCD module: one unit
  • A 74LS08 Quad AND Logic Gate Integrated Circuit: one unit
  • A 1N4001 general purpose silicon diode: one unit
  • A DC electric motor (3 V rated): one unit
  • Single-pole double-throw (SPDT) electric switches: two units
  • 1.5 V batteries: two units
  • 3 V battery holder: one unit
  • A breadboard
  • Wires

A programmable motor controller block diagram

The block diagram of the programmable DC motor controller with a Liquid Crystal Display (LCD) can be imagined as a remote control box with two slide switches and an LCD, as shown in following diagram:

 programmable-dc-motor-controller-lcd-img-0

The Remote Control Box provides the control signals to operate a DC motor. This box is not able to provide the right amount of electrical current to directly operate the DC motor. Therefore, a transistor motor driver circuit is needed. This circuit has sufficient current gain hfe to operate a small DC motor. A typical hfe value of 100 is sufficient for the operation of a DC motor. The Enable slide switch is used to set the remote control box to the ready mode. The Program switch allows the DC motor to be set to an ON or OFF operating condition by using a simple selection sequence. The LCD displays the ON or OFF selection prompts that help you operate the DC motor. The remote control box diagram is shown in the next image.

The idea behind the concept diagram is to illustrate how a simple programmable motor controller can be built by using basic electrical and electronic components. The Arduino is placed inside the remote control box and wired to the Enable/Program switches and the LCD. External wires are attached to the transistor motor driver, DC motor, and Arduino.

The block diagram of the programmable motor controller is an engineering tool that is used to convey a complete product design by using simple graphics. The block diagram also allows ease in planning the breadboard to prototype and test the programmable motor controller in a maker workshop or a laboratory bench. A final observation regarding the block diagram of the programmable motor controller is that the basic computer convention of inputs is on the left, the processor is located in the middle, and the outputs are placed on the right-hand side of the design layout. As shown, the SPDT switches are on the left-hand side, Arduino is located in the middle, and the transistor motor driver with the DC Motor is on the right-hand side of the block diagram.

The LCD is shown towards the right of the block diagram because it is an output device. The LCD allows visual selection between the ON/OFF operations of the DC motor by using Program switch. This left-to-right design method allows ease in building the programmable motor controller as well as troubleshooting errors during the testing phase of the project. The block diagram for a programmable motor controller is as follows:

 programmable-dc-motor-controller-lcd-img-1

Building the programmable motor controller

The block diagram of the programmable motor controller has more circuits than the block diagram of the sound effects machine. As discussed previously, there are a variety of ways to build the (prototype) electronic devices. For instance, they can be built on a Printed Circuit Board (PCB) or an experimenter's/prototype board. The construction base that was used to build this device was a solderless breadboard, which is shown in the next image. The placement of the electronic parts, as shown in the image, are not restricted to the solderless breadboard layout. Rather, it should be used as a guideline.

Another method of placing the parts onto the solderless breadboard is to use the block diagram that was shown earlier. This method of arranging the parts that was illustrated in the block diagram allows ease in testing each subcircuit separately. For example, the Program/Enable SPDT switches' subcircuits can be tested by using a DC voltmeter. Placing a DC voltmeter across the Program switch and 1 kilo ohm resistor and toggling switch several times will show a voltage swing between 0 V and +5 V. The same testing method can be carried out on the Enable switch as well. The transistor motor driver circuit is tested by placing a +5 V signal on the base of the 2N3904 NPN transistor. When you apply +5 V to the transistor's base, the DC motor turns on. The final test for the programmable DC motor controller is to adjust the contrast control (10 kilo ohm) to see whether the individual pixels are visible on the LCD. This electrical testing method, which is used to check the programmable DC motor controller is functioning properly, will minimize the electronic I/O wiring errors. Also, the electrical testing phase ensures that all the I/O circuits of the electronics used in the circuit are working properly, thereby allowing the maker to focus on coding the software. Following is the wiring diagram of programmable DC motor controller with the LCD using a solderless breadboard:

 programmable-dc-motor-controller-lcd-img-2

As shown in the wiring diagram, the electrical components that are used to build the programmable DC motor controller with the LCD circuit are placed on the solderless breadboard for ease in wiring the Arduino, LCD, and the DC motor. The transistor shown in the preceding image is a 2N3904 NPN device with a pin-out arrangement consisting of an emitter, a base, and a collector respectively. If the transistor pins are wired incorrectly, the DC motor will not turn on. The LCD module is used as a visual display, which allows operating selection of the DC motor. The program slide switch turns the DC motor ON or OFF. Although most of the 16-pin LCD modules have the same electrical pin-out names, consult the manufacturer's datasheet of the available device in hand. There is also a 10 kilo ohm potentiometer to control the LCD's contrast. On wiring the LCD to the Arduino, supply power to the microcontroller board by using the USB cable that is connected to a desktop PC or a notebook. Adjust the 10 kilo ohm potentiometer until a row of square pixels are visible on the LCD.

The Program slide switch is used to switch between the ON or OFF operating mode of the DC motor, which is shown on the LCD. The 74LS08 Quad AND gate is a 14-pin Integrated Circuit (IC) that is used to enable the DC motor or get the electronic controller ready to operate the DC motor. Therefore, the Program slide switch must be in the ON position for the electronic controller to operate properly. The 1N4001 diode is used to protect the 2N3904 NPN transistor from peak currents that are stored by the DC motor's winding while turning on the DC motor. When the DC motor is turned off, the 1N4001 diode will direct the peak current to flow through the DC motor's windings, thereby suppressing the transient electrical noise and preventing damage to the transistor. Therefore, it's important to include this electronic component into the design, as shown in the wiring diagram, to prevent electrical damage to the transistor. Besides the wiring diagram, the circuit's schematic diagram will aid in building the programmable motor controller device.

Let's build it!

In order to build the programmable DC motor controller, follow the following steps:

  1. Wire the programmable DC motor controller with the LCD circuit on a solderless breadboard, as shown in the previous image as well as the circuit's schematic diagram that is shown in the next image.
  2. Upload the software of the programmable motor controller to the Arduino by using the sketch shown next.
  3. Close both the Program and Enables switches. The motor will spin.
  4. When you open the Enable switch, the motor stops.

The LCD message tells you how one can set the Program switch for an ON and OFF motor control. The Program switch allows you to select between the ON and OFF motor control functions. With the Program switch closed, toggling the Enable switch will turn the motor ON and OFF. Opening the Program switch will prevent the motor from turning on. The next few sections will explain additional details on the I/O interfacing of discrete digital logic circuits and a small DC motor that can be connected to the Arduino.

A sketch is a unit of code that is uploaded to and run on an Arduino board.

/*
* programmable DC motor controller w/LCD allows the user to select ON and OFF 
operations using a slide switch. To * enable the selected operation another
slide switch is used to initiate the selected choice. * Program Switch wired to pin 6. * Output select wired to pin 7. * LCD used to display programming choices (ON or OFF). * created 24 Dec 2012 * by Don Wilcher */ // include the library code: #include <LiquidCrystal.h>   // initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2);   // constants won't change. They're used here to // set pin numbers: const int ProgramPin = 6;     // pin number for PROGRAM input control signal const int OUTPin = 7;     // pin number for OUTPUT control signal   // variable will change: int ProgramStatus = 0; // variable for reading Program input status     void setup() { // initialize the following pin as an output: pinMode(OUTPin, OUTPUT);   // initialize the following pin as an input: pinMode(ProgramPin, INPUT);   // set up the LCD's number of rows and columns: lcd.begin(16, 2);   // set cursor for messages andprint Program select messages on the LCD. lcd.setCursor(0,0); lcd.print( "1. ON"); lcd.setCursor(0, 1); lcd.print ( "2. OFF");   }   void loop(){ // read the status of the Program Switch value: ProgramStatus = digitalRead(ProgramPin);   // check if Program select choice is 1.ON. if(ProgramStatus == HIGH) {    digitalWrite(OUTPin, HIGH);   } else{      digitalWrite(OUTPin,LOW); } }

The schematic diagram of the circuit that is used to build the programmable DC motor controller and upload the sketch to the Arduino is shown in the following image:

 programmable-dc-motor-controller-lcd-img-3

Interfacing a discrete digital logic circuit with Arduino

The Enable switch, along with the Arduino, is wired to a discrete digital Integrated Circuit (IC) that is used to turn on the transistor motor driver. The discrete digital IC used to turn on the transistor motor driver is a 74LS08 Quad AND gate. The AND gate provides a high output signal when both the inputs are equal to +5 V. The Arduino provides a high input signal to the 74LS08 AND gate IC based on the following line of code:

   digitalWrite(OUTPin, HIGH);

The OUTPin constant name is declared in the Arduino sketch by using the following declaration statement:

const int OUTPin = 7;     // pin number for OUTPUT control signal

The Enable switch is also used to provide a +5V input signal to the 74LS08 AND gate IC. The Enable switch circuit schematic diagram is as follows:

 programmable-dc-motor-controller-lcd-img-4

Both the inputs must have the value of logic 1 (+5 V) to make an AND logic gate produce a binary 1 output. In the following section, the truth table of an AND logic gate is given. The table shows all the input combinations along with the resultant outputs. Also, along with the truth table, the symbol for an AND logic gate is provided. A truth table is a graphical analysis tool that is used to test digital logic gates and circuits. By setting the inputs of a digital logic gate to binary 1 (5 V) or binary 0 (0 V), the truth table will show the binary output values of 1 or 0 of the logic gate. The truth table of an AND logic gate is given as follows:

 programmable-dc-motor-controller-lcd-img-5

Another tool that is used to demonstrate the operation of the digital logic gates is the Boolean Logic expression. The Boolean Logic expression for an AND logic gate is as follows:

 programmable-dc-motor-controller-lcd-img-6

A Boolean Logic expression is an algebraic equation that defines the operation of a logic gate. As shown for the AND gate, the Boolean Logic expression circuit's output, which is denoted by C, is only equal to the product of A and B inputs. Another way of observing the operation of the AND gate, based on its Boolean Logic Expression, is by setting the value of the circuit's inputs to 1. Its output has the same binary bit value. The truth table graphically shows the results of the Boolean Logic expression of the AND gate.

A common application of the AND logic gate is the Enable circuit. The output of the Enable circuit will only be turned on when both the inputs are on. When the Enable circuit is wired correctly on the solderless breadboard and is working properly, the transistor driver circuit will turn on the DC motor that is wired to it. The operation of the programmable DC motor controller's Enable circuit is shown in the following truth table:

 programmable-dc-motor-controller-lcd-img-7

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at AU $24.99/month. Cancel anytime

The basic computer circuit that makes the decision to operate the DC motor is the AND logic gate. The previous schematic diagram of the Enable Switch circuit shows the electrical wiring to the specific pins of the 74LS08 IC, but internally, the AND logic gate is the main circuit component for the programmable DC motor controller's Enable function. Following is the diagram of 74LS08 AND Logic Gate IC:

 programmable-dc-motor-controller-lcd-img-8

To test the Enable circuit function of the programmable DC motor controller, the Program switch is required. The schematic diagram of the circuit that is required to wire the Program Switch to the Arduino is shown in the following diagram. The Program and Enable switch circuits are identical to each other because two 5 V input signals are required for the AND logic gate to work properly. The Arduino sketch that was used to test the Enable function of the programmable DC motor is shown in the following diagram:

 programmable-dc-motor-controller-lcd-img-9

The program for the discrete digital logic circuit with an Arduino is as follows:

// constants won't change. They're used here to
// set pin numbers:
const int ProgramPin = 6;   // pin number for PROGRAM input control signal
const int OUTPin = 7;     // pin number for OUTPUT control signal
 
// variable will change:
int ProgramStatus = 0;       // variable for reading Program input status
 
void setup() {
// initialize the following pin as an output:
pinMode(OUTPin, OUTPUT);
 
// initialize the following pin as an input:
pinMode(ProgramPin, INPUT);
}
 
void loop(){
 
// read the status of the Program Switch value:
ProgramStatus = digitalRead(ProgramPin);
 
// check if Program switch is ON.
if(ProgramStatus == HIGH) {
   digitalWrite(OUTPin, HIGH);
 
}
else{
     digitalWrite(OUTPin,LOW);
 
}
}

Connect a DC voltmeter's positive test lead to the D7 pin of the Arduino. Upload the preceding sketch to the Arduino and close the Program and Enable switches. The DC voltmeter should approximately read +5 V. Opening the Enable switch will display 0 V on the DC voltmeter. The other input conditions of the Enable circuit can be tested by using the truth table of the AND Gate that was shown earlier. Although the DC motor is not wired directly to the Arduino, by using the circuit schematic diagram shown previously, the truth table will ensure that the programmed Enable function is working properly.

Next, connect the DC voltmeter to the pin 3 of the 74LS08 IC and repeat the truth table test again. The pin 3 of the 74LS08 IC will only be ON when both the Program and Enable switches are closed. If the AND logic gate IC pin generates wrong data on the DC voltmeter when compared to the truth table, recheck the wiring of the circuit carefully and properly correct the mistakes in the electrical connections. When the corrections are made, repeat the truth table test for proper operation of the Enable circuit.

Interfacing a small DC motor with a digital logic gate

The 74LS08 AND Logic Gate IC provides an electrical interface between reading the Enable switch trigger and the Arduino's digital output pin, pin D7. With both the input pins (1 and 2) of the 74LS08 AND logic gate set to binary 1, the small 14-pin IC's output pin 3 will be High. Although the logic gate IC's output pin has a +5 V source present, it will not be able to turn a small DC motor. The 74LS08 logic gate's sourcing current is not able to directly operate a small DC motor.

To solve this problem, a transistor is used to operate a small DC motor. The transistor has sufficient current gain hfe to operate the DC motor. The DC motor will be turned on when the transistor is biased properly. Biasing is a technique pertaining to the transistor circuit, where providing an input voltage that is greater than the base-emitter junction voltage (VBE) turns on the semiconductor device. A typical value for VBE is 700 mV. Once the transistor is biased properly, any electrical device that is wired between the collector and +VCC (collector supply voltage) will be turned on. An electrical current will flow from +VCC through the DC motor's windings and the collector-emitter is grounded.

The circuit that is used to operate a small DC motor is called a Transistor motor driver, and is shown in the following diagram:

 programmable-dc-motor-controller-lcd-img-10

The Arduino code that is responsible for the operation of the transistor motor driver circuit is as follows:

void loop(){
 
// read the status of the Program Switch value:
ProgramStatus = digitalRead(ProgramPin);
 
// check if Program switch is ON.
if(ProgramStatus == HIGH) {
   digitalWrite(OUTPin, HIGH);
 
}
else{
     digitalWrite(OUTPin,LOW);
 
}
}

Although the transistor motor driver circuit was not directly wired to the Arduino, the output pin of the microcontroller prototyping platform indirectly controls the electromechanical part by using the 74LS08 AND logic gate IC. A tip to keep in mind when using the transistors is to ensure that the semiconductor device can handle the current requirements of the DC motor that is wired to it. If the DC motor requires more than 500 mA of current, consider using a power Metal Oxide Semiconductor Field Effect Transistor (MOSFET) instead.

A power MOSFET device such as IRF521 (N-Channel) and 520 (N-Channel) can handle up to 1 A of current quite easily, and generates very little heat. The low heat dissipation of the power MOSFET (PMOSFET) makes it more ideal for the operation of high-current motors than a general-purpose transistor. A simple PMOSFET DC motor driver circuit can easily be built with a handful of components and tested on a solderless breadboard, as shown in the following image. The circuit schematic for the solderless breadboard diagram is shown after the breadboard image as well. Sliding the Single Pole-Double Throw (SPDT)switch in one position biases the PMOSFET and turns on the DC motor. Sliding the switch in the opposite direction turns off the PMOSFET and the DC motor.

 programmable-dc-motor-controller-lcd-img-11

Once this circuit has been tested on the solderless breadboard, replace the 2N3904 transistor in the programmable DC Motor controller project with the power-efficient PMOSFET component mentioned earlier.

As an additional reference, the schematic diagram of the transistor relay driver circuit is as follows:

 programmable-dc-motor-controller-lcd-img-12

A sketch of the LCD selection cursor

The LCD provides a simple user interface for the operation of a DC motor that is wired to the Arduino-based programmable DC motor controller. The LCD provides the two basic motor operations of ON and OFF. Although the LCD shows the two DC motor operation options, the display doesn't provide any visual indication of selection when using the Program switch. An enhancement feature of the LCD is that it shows which DC motor operation has been selected by adding a selection symbol. The LCD selection feature provides a visual indicator of the DC motor operation that was selected by the Program switch. This selection feature can be easily implemented for the programmable DC motor controller LCD by adding a > symbol to the Arduino sketch.

After uploading the original sketch from the Let's build it section of this article, the LCD will display two DC motor operation options, as shown in the following image:

 programmable-dc-motor-controller-lcd-img-13

The enhancement concept sketch of the new LCD selection feature is as follows:

 programmable-dc-motor-controller-lcd-img-14

The selection symbol points to the DC motor operation that is based on the Program switch position. (For reference, see the schematic diagram of the programmable DC motor controller circuit.)

The partially programmable DC motor controller program sketch that comes without an LCD selection feature

Comparing the original LCD DC motor operation selection with the new sketch, the differences with regard to the programming features are as follows:

void loop(){
 
// read the status of the Program Switch value:
ProgramStatus = digitalRead(ProgramPin);
 
// check if Program switch is ON.
if(ProgramStatus == HIGH) {
   digitalWrite(OUTPin, HIGH);
 
}
else{
   digitalWrite(OUTPin,LOW);
 
}
}

The partially programmable DC motor controller program sketch with an LCD selection feature

This code feature will provide a selection cursor on the LCD to choose the programmable DC motor controller operation mode:

// set cursor for messages and print Program select messages on the LCD.
lcd.setCursor(0,0);
lcd.print( ">1.Closed(ON)");
lcd.setCursor(0, 1);
lcd.print ( ">2.Open(OFF)");
 
 
void loop(){
 
// read the status of the Program Switch value:
ProgramStatus = digitalRead(ProgramPin);
 
// check if Program select choice is 1.ON.
if(ProgramStatus == HIGH) {
   digitalWrite(OUTPin, HIGH);
     lcd.setCursor(0,0);
     lcd.print( ">1.Closed(ON)");
     lcd.setCursor(0,1);
     lcd.print ( " 2.Open(OFF) ");
 
}
else{
     digitalWrite(OUTPin,LOW);
     lcd.setCursor(0,1);
     lcd.print ( ">2.Open(OFF)");
     lcd.setCursor(0,0);
     lcd.print( " 1.Closed(ON) ");
 
}
}

The most obvious difference between the two partial Arduino sketches is that the LCD selection feature has several lines of code as compared to the original one. As the slide position of the Program switch changes, the LCD's selection symbol instantly moves to the correct operating mode. Although the DC motor can be observed directly, the LCD confirms the operating mode of the electromechanical device. The complete LCD selection sketch is shown in the following section. As a design-related challenge, try displaying an actual arrow for the DC motor operating mode on the LCD. As illustrated in the sketch, an arrow can be built by using the keyboard symbols or the American Standard Code for Information Interchange (ASCII) code.

/*
* programmable DC motor controller w/LCD allows the user to select ON and OFF operations using a slide switch. To
* enable the selected operation another slide switch is used to initiate the selected choice.
* Program Switch wired to pin 6.
* Output select wired to pin 7.
* LCD used to display programming choices (ON or OFF) with selection arrow.
* created 28 Dec 2012
* by Don Wilcher
*/
// include the library code:
#include <LiquidCrystal.h>
 
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
// constants won't change. They're used here to
// set pin numbers:
const int ProgramPin = 6;     // pin number for PROGRAM input control signal
const int OUTPin = 7;       // pin number for OUTPUT control signal
 
 
// variable will change:
int ProgramStatus = 0;       // variable for reading Program input status
 
 
void setup() {
// initialize the following pin as an output:
pinMode(OUTPin, OUTPUT);
 
// initialize the following pin as an input:
pinMode(ProgramPin, INPUT);
 
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
 
// set cursor for messages andprint Program select messages on the LCD.
lcd.setCursor(0,0);
lcd.print( ">1.Closed(ON)");
lcd.setCursor(0, 1);
lcd.print ( ">2.Open(OFF)");
 
}
 
void loop(){
 
// read the status of the Program Switch value:
ProgramStatus = digitalRead(ProgramPin);
 
// check if Program select choice is 1.ON.
if(ProgramStatus == HIGH) {
   digitalWrite(OUTPin, HIGH);
     lcd.setCursor(0,0);
     lcd.print( ">1.Closed(ON)");
     lcd.setCursor(0,1);
     lcd.print ( " 2.Open(OFF) ");
 
}
else{
     digitalWrite(OUTPin,LOW);
     lcd.setCursor(0,1);
     lcd.print ( ">2.Open(OFF)");
     lcd.setCursor(0,0);
     lcd.print( " 1.Closed(ON) ");
 
}
}

Congratulations on building your programmable motor controller device!

Summary

In this article, a programmable motor controller was built using an Arduino, AND gate, and transistor motor driver. The fundamentals of digital electronics, which include the concepts of Boolean logic expressions and truth tables were explained in the article. The AND gate is not able to control a small DC motor because of the high amount of current that is needed to operate it properly. PMOSFET (IRF521) is able to operate a small DC motor because of its high current sourcing capability. The circuit that is used to wire a transistor to a small DC motor is called a transistor DC motor driver. The DC motor can be turned on or off by using the LCD cursor selection feature of the programmable DC motor controller.

Resources for Article:


Further resources on this subject: