Skip to content

13. Output devices

This week I worked on controlling motor speed and direction.

Requirements for this project

the requirement for this project is to add an output device to a microcontroller board that I have designed, and program it to do something.

Wiring

  • Connect motor to motor Driver
  • Connect motor driver to microcontroller. |Motor driver Pin |Microcontroller Pin| |------------------|------------------| |VR |D5 | |Z/F |D6 | |Signal |D7 | |EL |D8 | |GND |GND | |5V |5V |
  • Connect microcontroller to raspberry PI with FTDI cable.
  • Connect touch screen to raspberryPi with USB and HDMI cable.

Programing

  • To program the microcontroller I have used Arduino UNO board connected with ISP header of the microcontroller.
Arduino UNO Pin # Microcontroller ISP Port
12 1-MISO
5V 2-VCC
13 3-SCK
11 4-MOSI
10 5-RST
GND 6-GND

  • Open Arduino IDE and go to File >> Examples >> Firmata >> StandardFirmata

Firmata is a protocol for communicating with microcontrollers from software on a computer. For more details you can check the GitHub page - To program the microcontroller go to Sketch and clink on Upload Using Programmer

  • Open Raspberry Pi terminal and install pyfirmata library for python by running the following command

    pip install pyfirmata

  • Open any Python Editor and write the following python code to control the motor

from tkinter  import *
import tkinter.font
from time import sleep
from pyfirmata import Arduino, util
from pyfirmata import INPUT, OUTPUT, PWM

arduinoPort = "/dev/ttyUSB0"
board = Arduino(arduinoPort)
sleep(2)

pin3 = board.get_pin("d:3:o")                           # LED Pin
motorPin = board.get_pin("d:5:p")                       # PWM pin connected to LED
motorEL = board.get_pin("d:8:o")                        # Motor Enable
motorZF = board.get_pin("d:6:o")                        # Motor Direction

win = Tk()
win.title("Motor Control")
myFont = tkinter.font.Font(family = "Helvetica", size =12, weight = "bold")

def ledToggle():
    if ledButton["text"] == "Turn Motor off":
        pin3.write(0)
        motorEL.write(0)
        ledButton["text"] = "Turn Motor ON"
    else:
        pin3.write(1)
        motorEL.write(1)
        ledButton["text"]= "Turn Motor off"

def close():
    win.destroy()

def speed(val):
    val = float(val)
    if val <0:
        val = abs(val) 
        motorZF.write(0)
        selection = "Backward = " + str(val)
    elif val >0:
        motorZF.write(1)
        selection = "Forward = " + str(val)
    else:
        selection = "Stop" 

    label.config(text = selection)    
    motorPin.write(val)
    sleep(0.01)

ledButton = Button(win, text="Turn Motor ON", font=myFont, command=ledToggle, bg="bisque2", height=1, width=24)
ledButton.grid(row=0,column=1)

var = DoubleVar()
speedScale = Scale(win, from_=-0.5, to=0.5, resolution = 0.05, variable = var, command = speed, orient=HORIZONTAL, length = 500, width = 50 )
speedScale.grid(row=1,column=1)

label = Label(win)
label.grid(row=2, column = 1)

exitButton = Button (win,text = "Exit", font = myFont, command = close, bg = "red", height = 1, width = 10)
exitButton.grid(row=10,column=1)

win.protocol("WM_DELETE_WINDOW", close)
win.mainloop()
  • You can download the full python code from here

  • Run the python code

  • Click on Turn Motor ON and move the slider to control the motor speed and direction

This is how it works:

Group assignment:

Measure the power consumption of an output device Document your work (in a group or individually)

For the Group Assignment, I’ll measure the power consumption for the motor. which will be used for my final project.

I have installed Two motors in my Robot and check the current consumption.

  • First test in desk without load.

Power = Current x Voltage = 0.07A x 24.4V =1.708W

Power for each motor = 1.708 / 2 = 0.854W

  • Second test with load and robot moving in the floor.
Power = Current x Voltage = 0.27A x 24.4V =6.588W

Power for each motor = 6.588 / 2 = 3.294W

Last update: October 24, 2021