Save the code and then run it from your editor (IDLE Run > Run Module/Thonny Run > Run Current Script) and after a few seconds the LED connected to the Arduino will flash, proving that we have a working connection. Then we sleep for 0.2 seconds, before turning the pin off and sleeping once more. Note that we use the variable led to identify the pin. We will call the object board, with a class to control the pin digitally (0,1) and then write 1 to the pin to turn it on. Inside of a while True loop, we can write the code that will turn the LED on and off every 0.2 seconds. You create it by adding the line: led = 12 board = Arduino('/dev/ttyUSB0')Ī variable called led is used to store the Arduino pin number. For this, we shall need to use the USB device information from dmesg. The next step is to create an object called board that will be the connection from our Pi to the Arduino. We can then import the sleep function from the time library, by typing: from pyfirmata import Arduino, util Start by importing two classes from the pyFirmata library, which will enable our code to connect to the Arduino. We shall now write some Python code into this file. Using your favourite Python 3 editor (IDLE, Thonny, nano, Vim), create a new file and name it LED_test.py. Look for USB devices such as ttyUSB0 and ttyACM0. Plug in the Arduino, and in the terminal type the following code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |