Arduino:workshop

From Juneday education
Jump to: navigation, search

Introduction

Arduino is a project, company and a user community that designs (and also manufactures) single boards and software to program these boards. You program these boards using an IDE called Arduino IDE, which comes with a library of useful functionality. The language is very like C and C++.

Arduino IDE

Arduino Web Editor

Other Development environments

Arduino language

Software

Install Arduino IDE (not the web editor) by following the installation instructions at: www.arduino.cc/en/Main/Software

Hardware

Arduino Kit When use this workshop at the Gothenburg University we're using an Arduino Kit with several different components. Apart from the Arduino board itself the kit consists of: breadboard leds, cables, resistors, buzzer, transistor and more.

Arduino-components.jpg In this workshop we're going to use the following components: arduino board, breadboard, cables and leds

Arduino and a breadboard

Arduino-board.jpg We have mounted the breadboard and the Arduino board on the plastic board. This makes it easy to connect wires between the Arduino board and components.

Leds

Arduino-leds.jpg A LED (light-emitting diode) is a component that we use as a lamp. A diode, in case you're interested, is a component that leads current in one direction. A led has two legs, one short and one slightly longer. WHen connection the led to the Arduino board, connect the shorter leg to ground and the longer to the pin that outputs "on" or "off" (a led can be used to present an analogue value as well, although faked, but for now we use it to present "on" or "off") .

and more

Well, we use cables as well. But we feel we have no need to explain what a cable is.

Wiring

Arduino-wire.jpg First of all we should connect the Arduino board with the breadboard. We could easily squeeze in a led on the Arduino board itself, but we want a hardware setup which you can continue working on after you've managed to get your first led blinking code to work. USe a cable (black in our example) to connect the GND pin on the Arduino board to the breadboard and another cable (yellow in our example) to connect pin number 13 to the breadboard. Pin 13 is normally used as an output pin.

Arduino-wired2.jpg Now we should connect the led between the GND pin and the output pin 13. Connect the longer leg on the led to pin 13 and the led's shorter pin to GND. There should be a small resistor between the output ping and the led, but we can skip this.

Connect Arduino and and your computer

Connect the USB cable to the Arduino board and your computer.

Use example code

Start the Arduino IDE

Arduino-example-menu.jpg Let's start of with an example that turn an output pin on and off every second. Open the Files menu and choose Examples ---> 0.1 Basics ---> Blink

Arduino-board-menu2.jpg There are several Arduino boards. We're using the Arduino UNO and need to make sure that the Arduino IDE is building code for that board. Open the Tools menu and choose Boards (could say Board Arduino/Genuino Uno) ---> Board Arduino/Genuino Uno

Arduino-blink2.png The code in the Arduino IDE window should look something like this. There are two steps left before we can enjoy a blinking led.

Build your code

The source code in your Arduino IDE windows needs to be compiled. To do this you click the leftmost button/icon in the Arudino IDE.

Upload the program

To upload the program (build from the source code) to the Arduino board you should click the the button/icon next to the build button. The led should now blink on and off every second.

Alter the blink program

Let's go crazy and alter the blink frequency. You can probably figure out yourself how to do this but if you want to cheat you can look at the code below:

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(100);                       // wait for a second
}

Dim a led

This time we should read input using a potentiometer and use that input to light a led.

Hardware setup

We're going to use the following pins:

  • A0 - to read input from potentiometer
  • 9 - to out analog (not digital as before) output to a led
  • 5V - to feed the potentiometer
  • GND - to ground the components

Here's a guide how to connect:

  • potentiometer:
    • connect 5V and GND (see picture below)
    • connect the mid pin to A0
  • led:
    • connect 9 to a small resistor
    • connect the resistor to the longer leg of the led
    • connect the shorter leg of the led to GND


Look at the picture to see how to connect the hardware. Led.jpg

Software

Let's use some variables to make our code more easily read:

int input_pin= A0; // Use A0 as input (from potentiometer)  
int led_pin = 9;  // Use pin 9 as output (for led, buzzer)


In Arduino we can set the hardware up using the function setup. We're going to use this to set up A0 as input pin and 9 as output pin. Here's a suggestion:

void setup() {
  pinMode(input_pin, INPUT); // input_pin should be set to input
  pinMode(led_pin, OUTPUT);  // led_pin should be set to output
}

Instead of a main function (or method) Arduino calls a function called loop(). This function is something you need to write, so let's do that. The function gets called "all the time" and per loop we should:

  • read the value from the potentiometer (A0)
  • calculate the output value (the input is between 0 and 1024 and the output should be between 0 and 255)
  • write the output value to the led pin (9)
void loop() {
 int input = analogRead(input_pin);
 analogWrite(led_pin, input*256/1024);
}

Complete source can be found here: potentio-led.ino

Trying things out

Twist the nob on the potentiometer and make sure that the led changes in intensity.

Buzzer

Replace the led with a buzzer (a piezo speaker) and see what happens when you twist the nob.

Let's rewrite the code a bit with the following in mind:

  • the input ranges between 0 and 1023
  • the tones most humans (the authors are excluded) can range between 200 and 20000 so let's use the input value 0...1023 to generate a tone with pitch 0...10230, i e multiply input with 10
  • the input signal will most likely vary (not be the same even though you do not change the potentiometer) so let's divide the input with 10 and then multiply with 100

Here's a suggested source code:

void loop() {
 int input = analogRead(input_pin);
 tone(led_pin, (input/10)*100, 10);
}

Complete source code can be found here: potentio-piezo.ino


Feedback

During the first workshop held 2018-12-21 we noticed the following:

  • connection between the (built and uploaded) code and what the Arduino does seems confusing
    • what is hardware and what is software
    • how to build and upload
  • harder to wire the hardware than expected
  • show more graphics to explain concepts: i e
    • signals ---> Arduino ---> signals
    • c code -> arduino
  • perhaps record some videos to get prepared?
  • perhaps a progression like:
    • light built in led
    • change frequency
    • use an external led
    • add breadboard
    • connect wires to breadboard and from there to the components
    • ....
  • Try this workshop again with our second year students?
    • during two lunches?