Linux Format

Get started with Ardiuino

We introduce a couple of easy projects to show you the bread(boards) and butter of being an Arduino maker.

-

Here we have two simple projects. The first tests that we can wire up a simple circuit, while the second project enables our Arduino to emulate a USB keyboard, which we will then use to quickly change the screen when our boss walks in. There are two ways to code your Arduino: via an online editor, or via the traditiona­l Arduino IDE (integrated developmen­t environmen­t).

Head in the clouds

Plug in your Arduino Nano 33 IOT, then in a browser visit https://create.arduino.cc and create a new account. Follow the instructio­ns, and when prompted download and install the Arduino Create app. The app will detect your Nano 33 IOT board and make it available to the Arduino IOT Cloud. You will be asked to set up the crypto chip on the Nano 33 IOT. This is essential for a secure connection to the Arduino IOT Cloud. If you are using another Arduino board, the installer will skip this section.

Down on the ground

If you wish to use the Arduino IDE, head over to https:// www.arduino.cc/en/main/software and download the IDE for your system. Then follow the online instructio­ns to install the IDE to your desktop. One issue you will face is that your user is not in the correct group to send data to the Arduino. So to add your user to the group, open a terminal and type the following (remember to change to your user name):

$ sudo usermod -a -G dialout

Then reboot your computer before proceeding.

In the tutorial we use the Arduino IOT Cloud, but the code is transferab­le between the two editors.

Project 1: Hello World

The most basic project we can create is to flash an LED. But why do we do that? Firstly it confirms that we can send data to the board, and secondly it confirms that our electronic components work.

Hardware setup

The circuit for the project involves an LED, breadboard, 330 Ohm resistor and two male to male jumper wires. The LED is placed into a breadboard, and the resistor is connected to the short leg (the cathode, –, GND leg) and to the GND pin of the Arduino via a jumper wire. The long leg of the LED (Anode, +) is connected to pin 2 of the Arduino via a jumper wire

Coding the project

In the Arduino IOT Cloud, click on Arduino Web Editor and then connect the Arduino to your computer. Create a new sketch in the Sketchbook. The first step is to create a function that will tell the Arduino that we wish to use pin 2 as an output to send current to the connected LED.

void setup() { pinmode(2, OUTPUT);

}

Our loop will run the code forever, and inside the main loop is a for loop, a loop that will iterate (go round) three times and quickly flash our LED before pausing.

void loop() { for (int i = 0; i <= 3; i++) {

To light up the LED we need to use digitalwri­te and then pass the pin number and the state of the pin. In this case we set pin 2 to HIGH, which will cause the LED to light up. After a short, 100ms delay, we then turn on the LED using LOW. Another delay occurs before the for loop goes back to the start again. Once the for loop has run three times, it will end and break out. A final delay of 1 second (1000ms) will then cause the LED to be off, and it will create a long pause in the flashing sequence.

digitalwri­te(2, HIGH); delay(100); digitalwri­te(2, LOW);

delay(100); } delay(1000); }

Click on the tick icon to check your code, and then click on the arrow to flash the code to the Arduino. In a few seconds the board will reboot and the LED will quickly flash three times, then pause, and repeat the process. You have successful­ly tested your Arduino and are now ready to move on to the next project.

Project 2 Job saver

In this project we’ll build a simple switch that when pressed will trigger a new tab to open in Firefox and

Chrome and open a page that looks like MS Outlook but is really Reddit in disguise. This is handy for work, school and home, but do so at your own risk!

Hardware setup

Using a breadboard we need to connect a pushbutton (momentary switch) over the central channel so that the legs are spread over the gap. Now, using two male to male jumper wires, connect the top-left leg of the button to GND on the Arduino, then connect the topright leg to pin 2 of the Arduino.

Coding the project

In the Arduino IOT Cloud, click on Arduino Web Editor and connect the Arduino to your computer. Create a new sketch in the Sketchbook, and the first step is to import the Keyboard library that will enable our Arduino to emulate a USB keyboard.

#include

We can type this into the code, or we can go to Libraries and then search for “Keyboard” and then click Include.

The next step is to configure the pin used for the button, and to start the keyboard interface. For this we will use a function called setup, which will run once, when the board is powered up. Using pinmode, we can tell the Arduino that we wish to use pin 2 as an input, and that the internal pull-up resistor should pull the pin high so that when the button is pressed it connects pin 2 (high) to GND. This will then pull pin 2 low and trigger the event.

void setup() { pinmode(2, INPUT_PULLUP); Keyboard.begin();

}

In order to continuall­y run our code, we need to create a loop that will contain the code. The first section of code in the loop is a test to see if the button has been pressed. Using digitalrea­d we can check pin 2, and if the pin is HIGH, the button has not been pressed. This means the code should take no action and keep waiting for input. We should use a 50ms delay to prevent accidental activation. void loop() { while (digitalrea­d(2) == HIGH) { delay(50);

}

But what happens if the button is pressed? In this case a delay is used to add 100ms delay before pressing a series of keys to open a tab. To press a key we use Keyboard.press and then pass the details of the key. For keys such as Ctrl, Alt, Shift we need to provide the details in a set manner, including whether it is the left or right of each key. For regular character keys (letters, numbers, punctuatio­n) we pass the key as a string. We then set a short 100ms delay to ensure that the keypresses are registered. delay(100); Keyboard.press(key_left_ctrl); Keyboard.press(‘t’); delay(100);

Right now the keys are pressed down, and if left too long this could cause issues. So to release the keys we use Keyboard.releaseall before moving on. When a new tab is opened in Firefox/chrome, the focus of the cursor will be in the address bar, and in the next line of code we’ll paste in a URL for the Reddit/outlook website. Then we release the keys once again before a final 100ms delay ends the loop.

Keyboard.releaseall(); Keyboard.println(“http://pcottle.github.io/

Msoutlooki­t/”); Keyboard.releaseall(); delay(100);

}

Click on the tick icon to check your code, and then click on the arrow to flash the code to the Arduino. In a few seconds the board will reboot and be ready for use. So go ahead, press the button and make sure that it works before the boss walks in!

The Arduino and Raspberry Pi are two very different products, but they both cater for eager hackers and makers. What if we could connect an Arduino to our Pi and use it as a slave device? One that reacts to input and sends the output to our Raspberry Pi via Python. To do this we are going to need some special software, and that is where this tutorial starts.

Software setup

Before we can write any Python code we need to download and install Arduino IDE for Linux ARM 32-bit from the Arduino website: find it at www.arduino.cc/ en/main/software.

Once installed we need to add the user Pi to the correct group to send data to the Arduino. So to add Pi to the group, open a terminal and type the following:

$ sudo usermod -a -G dialout pi

Then reboot your Raspberry Pi before continuing.

With the Pi rebooted, open Arduino IDE, and select File > Examples > Basic > Blink and then go to Tool > Board and select your board. Here we used an Arduino Uno. Then go to Tools > Port and make sure the port for your board is selected.

Now click on Sketch > Upload (or click on the arrow in the menu) to upload the code to the Arduino. After a few seconds the built-in LED of the Arduino should flash on/off slowly. This proves that we have a working unit.

With the test complete, we can now flash a special sketch that will enable us to talk to our Arduino with Python. Go to File > Examples > Firmata > Standardfi­rmata and flash this sketch to your Arduino. Once it is flashed, you can close the Arduino IDE.

To install the pyfirmata library, open a new terminal and type the following:

$ sudo pip3 install pyfirmata

Project Hello World

To test that our Arduino will work with Python we will write a quick script to turn on an LED connected to pin 12 of the Arduino. Please see the diagram for the connection­s (see right).

Plug in the Arduino, and in the terminal type the following code. Look for USB devices such as ttyusb0 and ttyacm0. Make a note before moving on.

$ dmesg

Using your favourite Python 3 editor (IDLE, Thonny,

nano, Vim) create a new file and name it Led_test.py.

We shall now write some Python code into this file. Start by importing two classes from the pyfirmata library, which will enable our code to connect to the Arduino. We can then import the sleep function from the time library, with:

from pyfirmata import Arduino, util from time import sleep

The next step is to create an object called board that will be the connection from our Pi to the Arduino. For this we shall need to use the USB device informatio­n from dmesg . In our case our Arduino was at ttyusb0 .

board = Arduino('/dev/ttyusb0')

A variable, led is used to store the Arduino pin number.

led = 12

Inside of a while True loop we can write the code that will turn the LED on and off every 0.2 seconds. 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. Note that we use the variable led to identify the pin. Then we sleep for 0.2 seconds, before turning the pin off and sleeping once more.

while True: board.digital[led].write(1) sleep(0.2) board.digital[led].write(0) sleep(0.2)

Save the code and then run the code 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.

Project 2: Flashing LEDS

Now let’s make a new project. This will be an LED that flashes, but the interval between each flash is controlled via a potentiome­ter, an analogue electronic component – something that the Raspberry Pi cannot ordinarily use without extra boards. We can use the value returned from the Arduino to control the speed at which the LED flashes. We will add the potentiome­ter to the existing LED test circuit that we have just built and tested. Please see the diagram (see right) for more informatio­n on this.

We’ll start the code for this project In a new blank file, using the same lines to import and configure the pin being used on the Arduino. from pyfirmata import Arduino, util from time import sleep board = Arduino('/dev/ttyusb0') led = 12

To read the analogue values from the Arduino we need to create a thread that will run and not interrupt the main code. We should create an object called it and then connect this to the Arduino, before then starting the thread. it = util.iterator(board) it.start() board.analog[0].enable_reporting()

The main body of code is a while True loop, which will read the current value of analogue pin 0, which is connected to the potentiome­ter, and store the value in a variable, value. This value will then be printed to the Python shell. while True: value = board.analog[0].read() print(value)

A conditiona­l test is now applied to the value

variable. If the value has no data, then it will return none, and this will crash the code. Therefore an if

condition checks the value, and if it is none, it changes

it to 0. if value == None:

value = 0

Another condition to test this is if the value is greater than 0.05, the analogue values returned are between 0.0 and 1.0. If the value’s greater than 0.05 the LED is turned on, and the sleep interval, used to keep the LED on/off by pausing the code, is controlled by the value. elif value > 0.05: board.digital[led].write(1) sleep(value) board.digital[led].write(0) sleep(value)

The final lines of code are an else condition, which turns the LED off if the value is less than 0.05. Save the code and run. Now turn the potentiome­ter and watch the LED come to life.

 ??  ?? The Arduino IDE uses a tick to verify that the code is correct. An icon with an arrow pointing right flashes the code to the board.
The Arduino IDE uses a tick to verify that the code is correct. An icon with an arrow pointing right flashes the code to the board.
 ??  ?? Your first project should always be simple, as it is a test to ensure that our code and hardware are working correctly.
Your first project should always be simple, as it is a test to ensure that our code and hardware are working correctly.
 ??  ?? Adding libraries to our code is simple via the Arduino IOT Cloud editor. Libraries offer additional features, such as USB keyboard emulation.
Adding libraries to our code is simple via the Arduino IOT Cloud editor. Libraries offer additional features, such as USB keyboard emulation.
 ??  ?? A button is used to trigger a series of events that opens a new browser tab and a website that looks like an email client.
A button is used to trigger a series of events that opens a new browser tab and a website that looks like an email client.
 ??  ?? An Arduino and Raspberry Pi working together over a USB cable and programmed in Python. We can build anything with this setup!
An Arduino and Raspberry Pi working together over a USB cable and programmed in Python. We can build anything with this setup!
 ??  ??
 ??  ?? To test that our project will work we need to use an LED to ensure that we can control devices using Python and pyfirmata.
To test that our project will work we need to use an LED to ensure that we can control devices using Python and pyfirmata.

Newspapers in English

Newspapers from Australia