Linux Format - UK (2020-03)

(Antfer) #1
42 LXF260 March 2020 http://www.linuxformat.com

Raspberry Pi projects


ere 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 traditional Arduino IDE (integrated development
environment).

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 instructions, 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://
http://www.arduino.cc/en/Main/Software and download the
IDE for your system. Then follow the online instructions
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
<username> to your user name):
$ sudo usermod -a -G dialout <username>
Then reboot your computer before proceeding.
In the tutorial we use the Arduino IoT Cloud, but the
code is transferable 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.

Get started with Ardiuino


We introduce a couple of easy projects to show you the


bread(boards) and butter of being an Arduino maker.


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 digitalWrite 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.
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);

H


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.

An Arduino
Nano 33 IoT or
Leonardo
A breadboard
2x M2M
jumper wires

Project 1
An LED
330 Ohm
resistor
(Orange-
Orange-Brown-
Gold)

Project 2
Push button
/ momentary
switch

Code: https://
github.com/
lesp/LXF260-
Arduino/
archive/
master.zip

YOU NEED


42 LXF260March 2020 3332March 0Sens2i0e

Raspberry Pi projects


erewehavetwosimpleprojects.Thefirsttests
thatwecanwireupasimplecircuit,whilethe
secondprojectenablesourArduinotoemulate
aUSBkeyboard,whichwewillthenusetoquickly
changethescreenwhenourbosswalksin.Thereare
twowaystocodeyourArduino:viaanonlineeditor,or
viathetraditionalArduinoIDE(integrateddevelopment
environment).

Headintheclouds
PluginyourArduinoNano 33 IoT,theninabrowservisit
https://create.arduino.ccandcreateanewaccount.
Followtheinstructions,andwhenprompteddownload
andinstalltheArduinoCreateapp.Theappwilldetect
yourNano 33 IoTboardandmakeitavailabletothe
ArduinoIoTCloud.Youwillbeaskedtosetupthecrypto
chipontheNano 33 IoT.Thisisessentialforasecure
connectiontotheArduinoIoTCloud.Ifyouareusing
anotherArduinoboard,theinstallerwillskipthissection.

Downontheground
IfyouwishtousetheArduinoIDE,headovertohttps://
http://www.arduino.cc/en/Main/Softwareanddownloadthe
IDEforyoursystem.Thenfollowtheonlineinstructions
toinstalltheIDEtoyourdesktop.Oneissueyouwillface
isthatyouruserisnotinthecorrectgrouptosenddata
totheArduino.Sotoaddyourusertothegroup,opena
terminalandtypethefollowing(remembertochange
<username>toyourusername):
$ sudousermod-a-Gdialout<username>
Thenrebootyourcomputerbeforeproceeding.
InthetutorialweusetheArduinoIoTCloud,butthe
codeistransferablebetweenthetwoeditors.

Project1: HelloWorld
Themost basic project we can create is to flash an LED.
Butwhydo we do that? Firstly it confirms that we can
senddata to the board, and secondly it confirms that
ourelectronic components work.

Get started with Ardiuino


We introduce a couple of easy projects to show you the


bread(boards) and butter of being an Arduino maker.


Hardwaresetup
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
theArduinoviaajumperwire

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 digitalWrite 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.
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);

H


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.

AnArduino
Nano 33 IoTor
Leonardo
Abreadboard
2x M2M
jumper wires

Project 1
An LED
330 Ohm
resistor
(Orange-
Orange-Brown-
Gold)

Project 2
Push button
/ momentary
switch

Code: https://
github.com/
lesp/LXF260-
Arduino/
archive/
master.zip

YOU NEED

Free download pdf