CodingHardwareHighlevel

Driving a Arduino Servo From python Tkinter Gui

In this manipulation we will see how to Drive a servo motor hooked into the Arduino pin #9, and that by giving it a real-time command from a Tkinter Slider.

Hardware

  • Arduino Uno board : Having the Firmata preloaded
  • Servo motor connected to the pin #9.
  • Serial medium: USB cable connected to a serial monitor or Pin#0(RX), Pin#1(TX) for Arduino Uno.

The Servo wiring

Servo-motor’s pins :

  • Yellow : PWM signal connected to pin #9
  • Red : VDD=+5V
  • Brown : Ground

Description

We will learn how to control the angular rotation of the servo motor in real time from the Python GUI, so let’s take a closer look. First, you need to set up the embedded software on your Arduino development board. It basically consists of standard. Firmata client logging program. Get there. You need to follow the steps below. First, connect your Arduino to your PC or Raspberry Pi, depending on your situation, and select the appropriate port name in the Arduino IDE. Then, from the IDE drop-down menu, you need to click File, Example, Firmata, and finally Standard Firmata. Finally, you need to compile and upload your code. The next step is to wire the servo to the Arduino board in hardware. Before you start, you should consider that most servos come with three wires: power supply, 5V, ground, and PWM signal or data wire. These three servo reads are usually provided in different color patterns. To distinguish between the three cables, here are some of the universal servo color codes. Once you have identified the three wires, connect them to your development board as follows: Servo ground and 5v will be zero and 5v on the board, respectively. For signal cables, you choose to connect to digital pin number 9, but you can connect to any digital pin that supports PWM mode, as in the case of the Arduino Uno.
Pin numbers 3, 5, 6. 9. Requires 10 or 11, and finally a USB serial port. In my case it’s COM7 for the Firmata protocol. For serial communication, you can first build a graphical user interface by implementing several libraries, the first library to handle the Firmata protocol, pyFirmata. The second library, Tkinter, helps create a graphical user interface and specifies the correct serial port (COM7 in my case). Then start the interactor thread to prevent the serial buffer from being compromised and overflowing, and finally set it up. Digital one-pin number nine. As output, and by code from Servo mode, a slider widget with a scaling function with up to 180 degrees, 400 pixels wide, horizontal alignment, and a callback function is now possible. Moveservo with an implicit argument is the actual value of the slider. Used to rotate the servo, and as a final touch, the tk constructor starts the top window label root and finally root.mainloop to keep the program running. Let’s finish the project, run this simulation and put into practice what we preached. This will launch the Arduino IDE, so here’s how to boot it. I already have the default company names, but I’ll show you how to get them…

Explainer video:

The Code

#!/usr/bin/python
# -*- coding: utf-8 -*-
# move a servo from a Tk slider - scruss 2012-10-28
 
import pyfirmata
from tkinter import *
 
# don't forget to change the serial port to suit
board = pyfirmata.Arduino('COM7')  #/dev/tty.usbmodem26271
 
# start an iterator thread so
# serial buffer doesn't overflow
iter8 = pyfirmata.util.Iterator(board)
iter8.start()
 
# set up pin D9 as Servo Output
pin9 = board.get_pin('d:9:s')
 
def move_servo(a):
    pin9.write(a)
 
# set up GUI
root = Tk()
 
# draw a nice big slider for servo position
scale = Scale(root,
    command = move_servo,
    to = 175,
    orient = HORIZONTAL,
    length = 400,
    label = 'Angle')
scale.pack(anchor = CENTER)
 
# run Tk event loop
root.mainloop()
  • Find All the code in the project Repository: