For this project we wanted to control a Lego vehicle with a Python Tkinter app. Next we added a short cut to the Pi desktop and then we used VNC to see the Pi desktop and our app on a tablet.
Our hardware components were:
- Raspberry Pi 3
- Pimoroni ExplorerHat Pro – supports bi-directional DC motors
- Dexter Connectors – allow 2 wire connections to Lego Mindstorms parts
- 2 Lego Mindstorms motors
- Portable USB charger
- lots of Lego parts
- 4 jumpers
The Lego Mindstorms parts are little pricey but they allow you to make some pretty funky contraptions. The other thing that we like about the Mindstorms motors is that they have a lot of torque for a 5V DC motor.
There are a few options for the cabling (like cutting the cable and exposing the individual wires) we used the Dexter connectors that are breadboard friendly. ANA and GND connections on the Dexter side go to Motor + and Motor – on the ExplorerHat Pro board.
The Tkinter library allows you to create a simple graphic user interface (GUI) with components like: buttons, sliders, lists, text, labels etc.
For our interface we created a grid of 3 rows and 2 columns with 5 buttons. We made a simple motor function where we passed the speed and direction of the wheels. A negative speed is backwards, zero is stop, and a positive speed is forward.
import Tkinter import explorerhat top = Tkinter.Tk() top.title("Car Control") explorerhat.motor.one.speed(0) explorerhat.motor.one.speed(0) #Define the buttons def motor(Left,Right): explorerhat.motor.one.speed(Right) explorerhat.motor.two.speed(Left) B_Left = Tkinter.Button(top, text ="Left", bg = "green", fg = "white", width= 15, height= 5, command = lambda: motor (50,0)).grid(row=1,column=1) B_Right = Tkinter.Button(top, text ="Right", bg = "green", fg = "white", width= 15, height= 5, command = lambda: motor (0,50)).grid(row=1,column=2) B_Forward= Tkinter.Button(top, text ="Forward", bg = "green", fg = "white", width= 15, height= 5, command = lambda: motor (50,50)).grid(row=2,column=1) B_Backward = Tkinter.Button(top, text ="Backward", bg = "green", fg = "white", width= 15, height= 5, command = lambda: motor (-50,-50)).grid(row=2,column=2) B_Stop = Tkinter.Button(top, text ="Stop", bg = "red", fg = "white", width= 33, height= 3, command = lambda: motor (0,0)).grid(row=3,column=1,columnspan=2) top.mainloop()
To create a Pi shortcut, create a file:
Inside this file define the name, path, and icon info for your new application:
[Desktop Entry] Name=Car Controls Comment=Python Tkinter Car Control Panel Icon=/home/pi/car1.png Exec=python /home/pi/mycarapp.py Type=Application Terminal=false Categories=None;
VNC (Virtual Network Computing)
VNC is install on the Raspbian image. To enable VNC run:
Then select the interfacing option, and then select VNC and enable.
Finally you will need to define a VNC password and load some VNC software on your Tablet. There are a lot of packages to choose from. We have an Android table and we used RemoteToGo without any problems.
Note, when your Pi boots without a HDMI monitor connected the desktop resolution will be at a low setting (probably 800×600) this can be adjusted. For us we simply resized the desktop to fit our tablet screen.