Arduino Programming

 

WEEK 4 (Tutorial)

 

In week 4 tutorial session, we were introduced to Arduino Programming.

 

Introduction to Arduino

 

Arduino is an open-source electronics platform based on easy-to-use hardware and software, used for building electronics projects. It consists of a physical programmable circuit board and a piece of software, or IDE (Integrated Development Environment). One can use the IDE to write a set of instructions or commands to be uploaded into the circuit board, also known as input, and the circuit board will be able to execute the command also known as output.

 

Maker Uno Edu Kit

Figure 1: Components of Maker Uno Board and their functions

Figure 2: USB Cable

The cable is used to connect the Maker Uno board to a computer with Arduino programming installed. This is so that the codes created in the computer can then be uploaded into the Maker Uno board. Once the code has been uploaded, the USB cable can be plugged in to any power source for it to perform the uploaded command. 


Figure 3: Jumper Wires

Jumper wires are used to connect electrical components to the Maker-Uno Board. They come in many different colours.

 

Activity 1: Hello World

The goal of this activity is to make one of the LEDs on the Maker-Uno Board blink. It can be accessed in the Arduino Programming software under File > Examples > 01. Basics > Blink.

 

The code used is shown below:

 

/*

 Blink

 

 Turns an LED on for one second, then off for one second, repeatedly.

 

 Most Arduinos have an on-board LED you can control. On the UNO, MEGA and

 ZERO

 it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to

 the correct LED pin independent of which board is used.

 If you want to know what pin the on-board LED is connected to on your Arduino

 model, check the Technical Specs of your board at:

 https://www.arduino.cc/en/Main/Products

 

 modified 8 May 2014

 by Scott Fitzgerald

 modified 2 Sep 2016

 by Arturo Guadalupi

 modified 8 Sep 2016

 by Colby Newman

 

 This example code is in the public domain.

 

 https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink

*/

 

// the setup function runs once when you press reset or power the board

void setup() {

  // initialize digital pin LED_BUILTIN as an output.

  pinMode(LED_BUILTIN, OUTPUT);

}

 

// 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(1000); // wait for a second

  digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

  delay(1000); // wait for a second

}

 

This code will cause the LED light at pin 13 to turn on and off at 1-second intervals.

 

In the code above, void setup(){} is where the board's pin identity is set up. For example, we can set PIN13 as an input or output, or simply make blink on the board's built-in LED light. void loop(){}is the part where we want the code to run on an infinite loop.

 

Activity 2: Programmable Button

 

The goal of this activity is to code the Maker-Uno Board so that when the programmable button is pressed, the LED at pin 13 will light up, and go off once the button is no longer being pressed. When the button is not pressed, the LED at pin 2 will be lit up permanently until the button is pressed again. It can be accessed under File > Examples> 02. Digital > DigitalInputPullup.

 

The code used is shown below:

 

/*

 Input Pull-up Serial

 

 This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital

 input on pin 2 and prints the results to the Serial Monitor.

 

 The circuit:

 - momentary switch attached from pin 2 to ground

 - built-in LED on pin 13

 

 Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal

 20K-ohm resistor is pulled to 5V. This configuration causes the input to read

 HIGH when the switch is open, and LOW when it is closed.

 

 created 14 Mar 2012

 by Scott Fitzgerald

 

 This example code is in the public domain.

 

 https://www.arduino.cc/en/Tutorial/BuiltInExamples/InputPullupSerial

*/

 

void setup() {

  //start serial connection

  Serial.begin(9600);

  //configure pin 2 as an input and enable the internal pull-up resistor

  pinMode(2, INPUT_PULLUP);

  pinMode(13, OUTPUT);

 

}

 

void loop() {

  //read the pushbutton value into a variable

  int sensorVal = digitalRead(2);

  //print out the value of the pushbutton

  Serial.println(sensorVal);

 

  // Keep in mind the pull-up means the pushbutton's logic is inverted. It goes

  // HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the

  // button's pressed, and off when it's not:

  if (sensorVal == HIGH) {

   digitalWrite(13, LOW);

  } else {

   digitalWrite(13, HIGH);

 }

}

 

When the button is pressed, the LED at pin 13 will light up, and go off once the button is no longer being pressed. When the button is not pressed, the LED at pin 2 will be lit up permanently until the button is pressed again.

 

In the code, IF a condition is met, the Maker-Uno Board will execute an action...ELSE it will perform another action... For the above code, IF sensorVal status is HIGH, PIN13 will be LOW, ELSE, it will be HIGH. In other words, if the button is not pressed, the light will be turned OFF, and if the button is press, the light will be ON.

 

Activity 3: Make Some Noise

 

The goal of the activity is to make the Maker-Uno Board play a quick melody on the Piezo Buzzer. It can be accessed under File > Examples> 02. Digital > toneMelody.

 

The code used is shown below:

 

/*

 Melody

 

 Plays a melody

 

 circuit:

 - 8 ohm speaker on digital pin 8

 

 created 21 Jan 2010

 modified 30 Aug 2011

 by Tom Igoe

 

 This example code is in the public domain.

 https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody

*/

 

#include "pitches.h"

 

// notes in the melody:

int melody[] = {

 NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4

};

 

// note durations: 4 = quarter note, 8 = eighth note, etc.:

int noteDurations[] = {

 4, 8, 8, 4, 4, 4, 4, 4

};

 

void setup() {

  // iterate over the notes of the melody:

  for (int thisNote = 0; thisNote < 8; thisNote++) {

 

   // to calculate the note duration, take one second divided by the note type.

   //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

   int noteDuration = 1000 / noteDurations[thisNote];

   tone(8, melody[thisNote], noteDuration);

 

   // to distinguish the notes, set a minimum time between them.

   // the note's duration + 30% seems to work well:

   int pauseBetweenNotes = noteDuration * 1.30;

   delay(pauseBetweenNotes);

   // stop the tone playing:

   noTone(8);

 }

}

void loop() {

  // no need to repeat the melody.

}

 

This will cause the buzzer to play a quick melody.

 

In the code above, #include "pitches.h" refers to the inclusion of all the pitches in a library so that the code can refer to the library if the code is being called. int melody[] is an array where integer information are stored in melody array. Similar to melody, int noteDurations[] stores the information of the note duration.

 

Activity 4: Servo

 

The goal of this activity is to make the rotor blade of a servo motor that is connected to the Maker-Uno Board spin. It can be accessed under File > Examples > Servo > Sweep.

 

The code used is shown below: 

 

/* Sweep

 by BARRAGAN <http://barraganstudio.com>

 This example code is in the public domain.

 

 modified 8 Nov 2013

 by Scott Fitzgerald

 https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep

 */

 

 #include <Servo.h>

 

 Servo myservo; // create servo object to control a servo

 // twelve servo objects can be created on most boards

 

 int pos = 0; // variable to store the servo position

 

 void setup() {

  myservo.attach(9); // attaches the servo on pin 9 to the servo object

 }

 

 void loop() {

  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees

   // in steps of 1 degree

   myservo.write(pos); // tell servo to go to position in variable 'pos'

   delay(15); // waits 15 ms for the servo to reach the position

  }

  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees

   myservo.write(pos); // tell servo to go to position in variable 'pos'

   delay(15); // waits 15 ms for the servo to reach the position

  }

}

 

This will cause the rotor to start spinning in a 180° arc.

 

In the code above, for (pos = 0; pos <= 180; pos += 1), this code sets pos = 0, and if pos is less or equals to 180, it will increment 1 by itself. For myservo.write(pos);, it will write the value of pos as and be sent to the servo motor. In delay (15);, this will then have a delay of 15 milliseconds.

 

This activity required 3 jumper wires and a servo motor. The jumper wires should be colour-coded so that we will know which wire should connect the Power, Data and Ground wires of the servo motor to the different pins on the Maker-Uno Board.

  • Bright colours (REDYELLOW, WHITE) for 5V
  • Dull colours (BLACK, GREYBLUE) for GND (ground)
  • Fun colours (ORANGEGREENPURPLE) for DATA

On the servo motor:

  • ORANGE - Data
  • RED  - +5V
  • BROWN  - GND

 

Below is the link to the videos of what the codes provided do, as well as modified versions of the codes provided:

https://drive.google.com/drive/folders/1--WqAaBAN6tyNcWg8XmRYEtMhtKQX9hU?usp=sharing

 

 

 

WEEK 5 (Practical)

 

For the Arduino practical, we went to T11C FabLab where we built a paper cardboard Pegasus and programmed a code to make its wings flap using a servo motor.


Figure 4: Cardboard Pegasus

In our original design, we placed the servo motor in the body of the Pegasus. However, this resulted in the wings not flapping very smoothly. The servo motor could be seen from underneath the body from certain angles. Thus, for the post-practical activity, our team leader Nick designed a way for the wings of the Pegasus to be able to flap smoothly, while hiding the servo motor and wires underneath it, but in a closed box this time. In this case, only the metal wire connecting the wings and the servo motor can be seen, and only if you are looking from the bottom perspective. Additional materials for this activity included an old shoebox, a power bank, and some metal wire.

 

For the code, we coded it such that the servo motor will rotate at an angle of 155 degrees with a delay of 7.5 seconds. This was the optimum code for our design, as other combinations would have resulted in a flapping animation that would be too clunky or unnatural.

 

The code that we used is shown below:

 

/* Sweep

 by BARRAGAN <http://barraganstudio.com>

 This example code is in the public domain.

 

 modified 8 Nov 2013

 by Scott Fitzgerald

 https://www.arduino.cc/en/Tutorial/LibraryExamples/Sweep

*/

 

#include <Servo.h>

 

Servo myservo;  // create servo object to control a servo

// twelve servo objects can be created on most boards

 

int pos = 0;    // variable to store the servo position

 

void setup() {

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

}

 

void loop() {

  for (pos = 0; pos <= 155; pos += 1) { // goes from 0 degrees to 155 degrees

    // in steps of 1 degree

    myservo.write(pos);              // tell servo to go to position in variable 'pos'

    delay(7.5);                       // waits 7.5 ms for the servo to reach the position

  }

  for (pos = 155; pos >= 0; pos -= 1) { // goes from 155 degrees to 0 degrees

    myservo.write(pos);              // tell servo to go to position in variable 'pos'

    delay(7.5);                       // waits 7.5 ms for the servo to reach the position

  }

}

 

For this activity, Nick glued the Pegasus to the shoebox, and in the shoebox, he placed the Maker-Uno Board containing the code and power bank. The servo motor was taped to the side of the shoebox.

Figure 5: Shoebox Set-up

The metal wire served as a connection between the servo motor and the Pegasus. One end of the metal wire would be tied around the wings of the Pegasus within the body, while the other end would be tied to the rotor blade on the servo motor.

 

Figure 6: Metal Wire Connection

Nick also made a slit in the shoebox to allow the metal wire to fit through.

Figure 7: Slit for Metal Wire

Figure 8: Final Set-up

TINKERCAD (INDIVIDUAL)

For the second individual activity, we were introduced to TinkerCad, a web-based simulation website that allows users to develop and test their own Arduino codes before uploading them to the Arduino Maker-Uno Board. It allows users to simulate how their experiments would work in real life, in a safe and virtual environment. It also identifies any mistakes in the code and allows users to modify them first before uploading them into the actual Maker-Uno Board.

 

Activity 1a: Interface a Potentiometer Analog Input to Maker-Uno Board and measure its signal in serial monitor Arduino IDE

 

Figure 9: Simulation and Code for Activity 1a

 

The code used is provided below:

 

// C++ code

//

int sensorValue = 0;

 

void setup()

{

  pinMode(A0, INPUT);

  pinMode(LED_BUILTIN, OUTPUT);

}

 

void loop()

{

  sensorValue = analogRead(A0);

  digitalWrite(LED_BUILTIN, HIGH);

  delay(sensorValue); // Wait for sensorValue millisecond(s)

  digitalWrite(LED_BUILTIN, LOW);

  delay(sensorValue); // Wait for sensorValue millisecond(s)

}

 

Below is the simulation for Activity 1a:

 Link to simulation: https://www.tinkercad.com/things/7oSjqbVb3H3



Activity 1b: Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE

Figure 10: Simulation and Code for Activity 1b




The code used is provided below:

// C++ code
//

int LDRValue = 0;  //LDRValue is an integer and starts from 0
void setup()
{
  pinMode(A0, INPUT);           //analog pin A0 is set as input
  pinMode(LED_BUILTIN, OUTPUT); //Builtin LED aka. pin13 is set as output
  Serial.begin(9600);           //Serial connection is established at 9600bits/s
}

void loop()
{
  LDRValue = analogRead(A0);  //Reads LDR value from A0
  Serial.println(LDRValue);   //Display LDRValue
  digitalWrite(LED_BUILTIN, HIGH); //turns on LED
}

Below is the simulation for Activity 1b:



Activity 2a: Interface 3 LEDs (Red, Yellow, Green) to maker UNO board and program it to perform something (fade or flash etc)

Figure 11: Simulation and Code for Activity 2a


The code used is provided below:

int animationSpeed = 0; //animationSpeed is set as an integer which

                                                                                          //is equal to 0

int brightness = 0;            //brightness is set as an  integer which is

                                                                                          //equal to 0

void setup()

{

  pinMode(11, OUTPUT);               //pin11 set as output

  pinMode(10, OUTPUT);  //pin10 set as output

  pinMode(9, OUTPUT);   //pin9 set as output

}

 

void loop()

{

  animationSpeed = 400;                                             //animationSpeed is set to 400ms

  for (brightness = 0; brightness <= 255; brightness += 17) {

    //brightness increases from 0 to 255 in the multiples of 17

    analogWrite(11, brightness);

    //tells pin11 the current brightness

  delay(animationSpeed); // Wait for animationSpeed millisecond(s)

  }

  for (brightness = 255; brightness >= 0; brightness -= 17) {

    //brightness decreases from 255 to 0 in the multiples of 17

    analogWrite(11, brightness);

    //tells pin11 the current brightness

  delay(animationSpeed); // Wait for animationSpeed millisecond(s)

  }

  for (brightness = 0; brightness <= 255; brightness += 17) {

    analogWrite(10, brightness);

  delay(animationSpeed);

  }

  for (brightness = 255; brightness >= 0; brightness -= 17) {

    analogWrite(10, brightness);

  delay(animationSpeed);

  }

  for (brightness = 0; brightness <= 255; brightness += 17) {

    analogWrite(9, brightness);

  delay(animationSpeed);

  }

  for (brightness = 255; brightness >= 0; brightness -= 17) {

    analogWrite(9, brightness);

  delay(animationSpeed);

  }

}

Below is the simulation for the activity:


Link for activity: https://www.tinkercad.com/things/0CCs86LjFPF


Activity 2b: Interface the DC motor to maker UNO board and program it to on and off using push button on the board

 

Figure 12: Simulation and Code for Activity 2b



The code used is provided below:

// C++ code

//

int ButtonState = 0; // setting ButtonState as an integer equal to 0

 

void setup()

{

  pinMode(2, INPUT);      //set pin2 as input

  pinMode(LED_BUILTIN, OUTPUT);          //set builtin LED as output

}

 

void loop()

{

  ButtonState = digitalRead(2);     //reads input at pin2

  if (ButtonState == HIGH) {          //if input ButtonState is high

    digitalWrite(LED_BUILTIN, HIGH); //output is high so motor starts moving

  } else {

    digitalWrite(LED_BUILTIN, LOW); //else, output is low

  }

  delay(10); // Delay for 10ms to improve simulation performance

}


Below is the simulation for this activity:

Link to simulation: https://www.tinkercad.com/things/aTEecVKlz1j


Reflection 

I found Arduino programming surprisingly fun. I had previously dismissed the idea of programming thinking it was difficult and too boring but having experienced it now, I find it very enjoyable. This is definitely beneficial, as the world is becoming increasingly digitalised, hence programming is an essential skill that everyone should acquire. 

Comments

Popular posts from this blog

Project Development

Design of Experiments (DOE)

Homepage