Have you ever wished to use Python instead of the Arduino language to control your Arduino board from your computer? The Firmata protocol-based Python library pyFirmata is now available.
I'll demonstrate how to set up PyFirmata and create entire programmes that run on actual hardware in this tutorial.
What you need:- LattePanda 3 Delta that's all.
You must upload the Firmata sketch in order to use that protocol to control the board before writing your Python programme to drive Arduino. The Arduino IDE's built-in examples include the sketch.
To open it, access the File menu, then Examples, followed by Firmata, and finally StandardFirmata.
Upload this StandardFirmata code to the Arduino IDE.
Then we are good to go to program by using the Python.
Step-2: Python InstallationOpen the python's official web site then download the latest version of python.
Next add the python to the path variable and install the tool.
Wait until the installation is done.
Next open the command prompt and type in python.
If you can see this response means, python is installed correctly.
Open the command prompt and enter the following command to install the PyFirmata pip install pyFirmata.
And wait until the installation is done.
That's all about the installation and setup. Now let's move on to the programming part.
Step-4: ProgrammingUse your preferred Python IDE and run the following code.
Note: Change the COM port to yours.
import pyfirmata
board = pyfirmata.Arduino('COM4')
board.digital[13].write(0)
You can see that it turns on the D13 led of the LattePanda 3 Delta.
Then change the write value to "0" it will turn off the D13 led.
Now let's try to get some Digital inputs. There are two logical values used to represent the digital inputs. 0 and 1.
In this following example will get a digital input from the D10 pin on the LattePanda 3 Delta then based on the value will blink the D13 led.
import pyfirmata
import time
board = pyfirmata.Arduino('COM4')
it = pyfirmata.util.Iterator(board)
it.start()
digital_input = board.get_pin('d:10:i')
led = board.get_pin('d:13:o')
while True:
sw = digital_input.read()
if sw is True:
led.write(1)
else:
led.write(0)
time. Sleep(0.1)
Using a push button to communicate the LP3 digital input values, you'll be able to control the LED. When the button is pressed and released, it should deliver 5V to the circuit board.
In here I'm using jumper instead of the push button.
Analogue inputs are used to read values within a range as opposed to digital inputs, which can only be on or off. The voltage to an analogue input on the LattePanda 3 Delta spans from 0V to 5V. To measure physical quantities like light and gas, the proper sensors are employed. These sensors are in charge of encoding these physical values in the correct voltage range so that the Arduino can read them.
In here I'm using light sensor to give analog input to the LattePanda 3 Delta.
import pyfirmata
import time
board = pyfirmata.Arduino('COM4')
it = pyfirmata.util.Iterator(board)
it.start()
analog_input = board.get_pin('a:0:i')
while True:
analog_value = analog_input.read()
print(analog_value)
time.sleep(0.5)
Here are my terminal values, you can see based on the light values it will give different analog reading.
Finally, I have created a simple GUI application, which can control the turn on/off the led. Here is the complete sketch for the GUI application.\
import tkinter as tk
import pyfirmata
board = pyfirmata.Arduino('COM4')
def on():
print("On")
board.digital[13].write(1)
def off():
print("Off")
board.digital[13].write(0)
def quit():
window.destroy()
window = tk.Tk()
window.title("LED Control")
window.geometry("300x100")
on_button = tk.Button(window, text="On",height=1, width=5,bg="green",fg="black", command=on)
on_button.place(x=50,y=30)
off_button = tk.Button(window, text="Off",height=1, width=5,bg="red",fg="black", command=off)
off_button.place(x=125,y=30)
quit_button = tk.Button(window, text="Quit",height=1, width=5,bg="yellow",fg="black", command=quit)
quit_button.place(x=200,y=30)
window.mainloop()
The LattePanda 3 Delta, which also includes an Arduino-enabled SBC, is an alternative to conventional PCs. Consequently, you can play and program on the same board. This article demonstrated how to use PyFirmata with the LP3. We look forward to seeing you in another tutorial. Thanks.
Comments