Linux Format

Get started with Arduino

Learn the Arduino language and see what this tiny microcontr­oller can do.

-

Occasional­ly we get chagrinned readers writing in with complaints about too much Raspberry Pi content. So here’s something different ( hold ontoyourha­ts–Ed).

Unlike the Raspberry Pi, Arduino devices aren’t potent enough to run a full-blown Linux distributi­on. For example, the popular Arduino Uno is powered by a 16MHz chip and has a total of 32KB of flash memory and only 2KB of SRAM (where your programs, known as sketches, get uploaded). However, they do have a lot of input and output pins, so their main utility is as microcontr­ollers: taking sensor readings and switching on LEDs, spinning motors or any number of other things in response. More powerful Arduinos exist, but even those rely on other devices for heavy computatio­n, networking and storage. You can download the Arduino IDE from https:// arduino.cc, or if you’re using Ubuntu you can do it via Ubuntu-Make, which will fetch the latest version. Do sudo apt install ubuntu-make then umake ide arduino . You may need to add your user to the dialout group in order to communicat­e with the Arduino ( Ubuntu-Make does this for you, but you’ll still need to log out and log back in again for this to take effect). If you’re feeling avant garde, you can also use the web editor from any modern browser on any platform by registerin­g at https://create.arduino.cc/editor.

Don't blink

However you do it, let’s follow the traditiona­l pathway and load the Blink example. Go to File>Examples> Basics Blink and have a look at the code. We can see it’s divided into two functions: setup() and loop() . In the first we define the LED as an output. It’s hard to see how this could be any other way, but we still need to specify such things. LED_BUILTIN is a constant correspond­ing to pin number to which the LED is connected (usually 13). We need to specify a mode before we can use any pin. Moving on to the loop function, we can see this sets our LED voltage high, waits for one second, sets it low again, waits another second, and continues in this manner indefinite­ly. Click the Upload button in the toolbar and see that this is indeed what happens.

If we want to read values from our Arduino program on the host machine, we can do so over the serial connection. We need to set the serial connection up by adding the line Serial.begin(9600); to the setup()

function. This means we can send a whopping 9,600 bits per second over the USB cable, which is the same speed as modems of the early 90s sent data over phone lines. Suppose we want to verify the statement above about the LED being connected to pin 13. Then all we need to do is add the following line: Serial.println(LED_BUILTIN); to the loop() function. That’s a lowercase 'L' in println

(short for print LiNe) by the way. Now when we upload

the sketch the LED will blink as before, but if we go to Tools>Serial Monitor we can see the value 13 being spat out once every two seconds. If you have an exotic Arduino you may see a different number here.

Sensor on a servo

For this project we’re using the breadboard just to distribute power. Our servo (Towerpro MG699R) is rated at 500-900mA while moving, which is a fair chunk of power. Its stall current (when it’s operating at maximum torque, i.e. its movement is being opposed) is a tremendous 6A, but we won’t be doing any stalling. Again though, it’s important your Arduino has adequate power and for this project you should connect a PSU to the barrel connector rather than powering by USB. The signal pin on our servo connects to pin 9 on the Arduino

The HC-SR04 ultrasonic sensor is a fantastic piece of kit. Send a pulse on its trigger line, and it emits a high-frequency squeak. If this squeak bounces off a nearby object, then when its echo returns to the sensor the echo pin will sent a 5V pulse lasting as long as it took between sending and receiving the ultrasonic pulse. This timing, combined with the speed of sound (around 330 m/s in air) enables us to determine the distance between the sensor and the object it just squeaked at. This is very much like the echolocati­on used by bats, except bats work on a much more complicate­d range of voltages and are furry.

By combining the servo and the ultrasonic sensor, we can do some quite fantastic things. The following code, for example, will sweep the sensor left and right. This makes for a kind of radar contraptio­n: const int trigPin = 10; const int echoPin = 11;

Servo myServo; void setup() {

Serial.begin(9600) myservo.attach(9);

We use the serial link to get the rotation of the servo and again this can be viewed from the Serial Monitor. From here we can add some code to get data from the sensor informatio­n and instead send this, or better yet the distance calculated therefrom as well. We’ll leave this as an exercise, or just check the link below.

There isn’t space to go any further with this, but the possibilit­ies are endless. We were particular­ly inspired by the Object Detector project described at http://bit. ly/object-detector. There, an authentic green radar display is rendered using the Processing language. The drawing part is pretty involved, and features a nice blurring effect as the sensor sweeps left and right, but the underlying hardware is just the same. It’s a great example of gathering data on the Arduino, and then processing and visualisin­g it on more capable hardware.

 ??  ?? There are heaps of categorise­d examples for you to discover in the IDE. Just head to File>Examples and fill your boots.
There are heaps of categorise­d examples for you to discover in the IDE. Just head to File>Examples and fill your boots.
 ??  ?? Even simple projects with buzzers and switches can look messy, but trust us when we say you’ll get used to this.
Even simple projects with buzzers and switches can look messy, but trust us when we say you’ll get used to this.
 ??  ?? It works – our sensor is sending back a stream of data about nearby objects at various bearings. Whoop!
It works – our sensor is sending back a stream of data about nearby objects at various bearings. Whoop!

Newspapers in English

Newspapers from Australia