Maximum PC

Getting Started with Arduino

-

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 the Arduino: via an online editor, or via the traditiona­l Arduino IDE (integrated developmen­t environmen­t).

Plug in the 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 the board and make it available to the Arduino IoT Cloud. You’ll be asked to set up the crypto chip on the Nano 33 IoT; this is essential for a secure connection to the cloud. If you’re using another Arduino board, the installer will skip this section.

If you wish to use the Arduino IDE, go to 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 is that your user may not be not be in the correct group to send data to the Arduino; to fix this, open a terminal and type the following (change < username> to your user name):

$ sudo usermod -a -G dialout

Then reboot your computer before proceeding. In this tutorial we will be using the Arduino IoT Cloud, but the code is transferab­le between the two editors. –JONNI BIDWELL

1

PROJECT 1: HELLO WORLD

The most basic project we can create is to flash an LED. But why do that? Well, it confirms that we can send data to the board, and it also confirms that our electronic components work [ Image A]. 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 another jumper wire.

2

CODING THE PROJECT

In the Arduino IoT Cloud, click “Arduino Web Editor” and then connect the Arduino to your computer. Create a new sketch in “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, which 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 one 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 the tick icon to check your code [ Image B], 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. Congratula­tions! You have successful­ly tested your Arduino.

3

PROJECT 2: JOB SAVER

Here we’ll build a simple switch that when pressed will trigger a new tab to open in Firefox or Chrome, opening a page that looks like MS Outlook but is really Reddit in disguise. It’s handy for work, school, or home, but do so at your own risk!

>> Using a breadboard, we need to connect a push button (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 top-right leg to pin 2 of the Arduino.

4

CODING THE PROJECT

In the Arduino IoT Cloud, click “Arduino Web Editor” and connect the Arduino to your computer. Create a new sketch in “Sketchbook”. Now, import the “Keyboard” library that will enable our Arduino to emulate a USB keyboard [ Image C].

#include

We can type this into the code, or we can go to “Libraries”, then search for “Keyboard” and 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 pull pin 2 low and trigger the event. void setup() {

pinMode(2, INPUT_PULLUP);

Keyboard.begin(); }

>> 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);

}

>> 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 pass the details of the key. For keys such as Ctrl, Alt, Shift 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 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. We then 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 [ Image D] and make sure that it works before the boss walks in!

 ??  ?? A
A
 ??  ?? B
B
 ??  ?? D
D
 ??  ?? C
C

Newspapers in English

Newspapers from United States