Arduino Programming

Week 4 (Tutorial)

For the tutorial in week 4, we were introduced to Arduino Programming.

Background Information
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 (often referred to as a microcontroller) and a piece of software, or IDE (Integrated Development Environment). One can use the IDE to write a set of instructions (commands) to be uploaded into the circuit board (input), and the circuit board will be able to execute the command (output).

Parts of the Maker Uno Edu Kit
Figure 1: Parts of an Arduino Maker-Uno Board

  1. This is the Micro USB B Type Connector. This USB connection is how the Maker-Uno Board will be connected to a computer or power source.
  2. The Reset Button is used to restart the Maker-Uno Board from scratch. Any code that has been uploaded into the Maker-Uno Board will be forgotten by the board.
  3. This is the Programmable Button. It can be used in the code by configuring it to INPUT_PULLUP.
  4. This is a series of LEDs that can be used by digital inputs as outputs to be controlled by the code.
  5. These are PWM (Pulse Weave Modulation) pins (3, 5, 6, 9, 10, 11), represented by the symbol "~". These pins can be used to simulate an analog output by configuring them to analogWrite;.
  6. These digital pins can be used for both digital inputs (digitalRead();) and digital outputs (digitalWrite();).
  7. Analog pins can be used to read analog inputs and convert them into digital inputs by configuring the code to analogRead();.
  8. These are power pins. The Ground (GND) pin is used to ground the circuit, while the 3.3V and 5V pins will supply 3.3 and 5 volts to the circuit, respectively.
  9. The Piezo Buzzer is connected to pin 8. It can be programmed to produce melodies.
  10. The Piezo Buzzer Slide Switch controls the Piezo Buzzer. It can be switched off if pin 8 is required for other purposes.
Figure 2: USB Cable

The cable (USB Micro B) will connect the Maker-Uno Board to a computer with the Arduino Programming software. This connection allows the codes to be uploaded from the computer into the board. Once the code has been uploaded, it can be unplugged from the computer and plugged into a power source using the same cable.

Figure 3: Jumper Wires

These 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 (RED, YELLOW, WHITE) for 5V
  • Dull colours (BLACK, GREY, BLUE) for GND (ground)
  • Fun colours (ORANGE, GREEN, PURPLE) for DATA
On the servo motor:
  • ORANGE - Data
  • RED  - +5V
  • BROWN  - GND
Week 5 (Practical)

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

Figure 4: Paper Cardboard Pegasus

Our original design had the servo motor hiding underneath the body, but this resulted in a rather clunky flapping animation. Furthermore, the servo motor could be seen from underneath the body from certain angles. Thus, for the post-practical activity at home, I designed a way for the wings of the Pegasus to be able to flap, 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, I coded it such that the servo motor would rotate at an angle of 155 degrees, with a delay of 7.5 seconds. This was the optimum code for my design, as other combinations would have resulted in a flapping animation that would be too clunky or unnatural.

The code that I 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, I glued the Pegasus to the shoebox, and in the shoebox, I 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 Setup

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

I made a slit in the shoebox to allow the metal wire to fit through.
Figure 7: Slit for Metal Wire

Figure 8: Final Setup

The link above contains the video for the final flapping animation of the paper cardboard Pegasus. It also contains the videos on the activities carried out during the tutorial, as well as some videos with modified codes. The codes for these modified codes can be found in the document.


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

Code used:

// 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 this activity:  
This is the link to the simulation: https://www.tinkercad.com/things/2Iip9uY50Cu

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

Code used:

// 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 this activity:

This is the link to the simulation: https://www.tinkercad.com/things/8ehOypXLDhY

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

Code used:

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 this activity:

This is the link to the simulation: https://www.tinkercad.com/things/gZcL4F6AWNU

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

Code used:

// 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:

This is the link to the simulation: https://www.tinkercad.com/things/1pUrCHZiwFy

Reflection
In conclusion, coding and programming is actually surprisingly fun. While it may look daunting at first, due to how alien it looks, once we get the hang of it, we begin to have a better understanding of how to use and apply it in different contexts to serve different purposes. I feel that this lesson is a very useful one, as many products nowadays are becoming more automated and technological, and a lot of coding goes into making these products work. Many companies would require more programmers to write the codes for them to function properly. I think Arduino programming can be applied into future CPDD projects to make it simpler for us to make our product work.

References
Arduino.cc. n.d. Arduino - Introduction. [online] Available at: <https://www.arduino.cc/en/guide/introduction> [Accessed 26 November 2021].
Learn.sparkfun.com. n.d. What is an Arduino? - learn.sparkfun.com. [online] Available at: <https://learn.sparkfun.com/tutorials/what-is-an-arduino/all> [Accessed 26 November 2021].

Comments

Popular posts from this blog

Project Development

Home