Skip to main content

Arduino Programming

Arduino Programming

Hello, welcome back to my blog! ๐Ÿ˜Š  This is another exciting ☺ week as I will be embarking on my Arduino Programming learning journey. Arduino Programming is a simple hardware programming that uses Arduino programming language to run a programme. I'll be sharing my learning progress using Arduino IDE, Tinkercad simulation and Maker Uno Kit on some assignments and the practical done in school.

Figure 1: Arduino Maker UNO Kit

1. INPUT Devices

a)  Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial            monitor Arduino IDE.

To start with this question, you need to know what is the use of a potentiometer in Arduino. The potentiometer is a variable resistor that comes with a knot to adjust the resistance. An example would be to adjust the rate at which an LED blinks. Next, we will set up the circuit on Tinkercad.

Figure 2: Potentiometer Analog Input Set Up on TinkerCad
       Simulation of Potentiometer on Tinkercad
Click HERE for the simulation of the potentiometer on Tinkercad

As seen from the circuit that is set up, the red wire is connected to a 5V pin which provides power. The green wire is connected to the Analog pin A0 which can measure 0-5V value depending on the potentiometer resistance. Analog-to-Digital Converter ADC will convert the input voltage ranging from 0-5V to a digital value between 0 and 1023. The black wire is connected to GND which is ground. These three wires are connected to the potentiometer to give analogue input of the potentiometer. Then, the yellow wire is connected to digital PIN 13 which is the default LED pin and is defined as the output LED. A resistor and yellow wire is then connected to the LED. This completes the setup of the potentiometer analog input.

However, this is just the setting up, programming needs to be done to run the simulation. The coding from Tinkercad does not show the serial monitor because serial communication is not set up. Thus, I modify the coding so that signals can be measured in a serial monitor. Below shows the Arduino coding and comments for the simulation before and after modification. The modified parts are highlighted in yellow.

Coding from Tinkercad

Modified Coding

int sensorValue = 0;
 
void setup()
{
  pinMode(A0, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}
 
void loop()
{
  // Read the value from the sensor
  sensorValue = analogRead(A0);
  // turn the LED on
  digitalWrite(LED_BUILTIN, HIGH);
  // pause the program for <sensorValue> milliseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
  // turn the LED off
  digitalWrite(LED_BUILTIN, LOW);
  // pause the program for <sensorValue> milliseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
}

 

int sensorValue = 0;
 
void setup()
{
  pinMode(A0, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}
 
void loop()
{
  // Read the value from the sensor
  sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  // reading sensor value
  // turn the LED on
  digitalWrite(LED_BUILTIN, HIGH);
  // pause the program for <sensorValue> milliseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
  // turn the LED off
  digitalWrite(LED_BUILTIN, LOW);
  // pause the program for <sensorValue> milliseconds
  delay(sensorValue); // Wait for sensorValue millisecond(s)
}

 

Click THIS to download the code

Figure 3: Screenshot of Serial Monitor during  Potentiometer Simulation

Video 1: Potentiometer with LED on Maker UNO


b)  Interface a LDR to maker UNO board and measure its signal in serial monitor Arduino IDE.

Light Dependent Resistor (LDR) is a component in which it changes resistance depending on the light intensity that it captures. It can be used in light sensing circuits such as street lights. If you observe closely, the lights on the street is switched off during the day and turned on at night. It works by using LDR to sense the light intensity so that when the light intensity is low, resistance is high, this caused the voltage across the base to be low, causing the light to turn on.  Now that you know what an LDR is, let's set up the circuit on Tinkercad! ๐Ÿ˜€

Figure 4: Set up of LDR Circuit on Tinkercad

Simulation of LDR on Tinkercad

Click HERE for the simulation of LDR on Tinkercad

As seen from the circuit, the red wire is connected to a 5V pin which provides power. The green wire is connected to the Analog pin A0 which can measure 0-5V value depending on the LDR resistance. Analog-to-Digital Converter ADC will convert the input voltage ranging from 0-5V to a digital value between 0 and 1023. The black wire is connected to GND which is ground. These three wires are connected to the LDR to give analogue input of the LDR. Pin 9 is declared as the input for the LED. It is connected together with a resistor. LED works from 0 to 255 so to let the LED light up at night, the range of values is reversed to 255 to 0 where when the LDR resistance is high, the brightness of LED will be at maximum.

Programming needs to be set up to allow the circuit to work. The coding and explanation is reflected in the table below.

LDR Coding

int PhotoSensor = 0;
 
void setup()
{
  pinMode(A0, INPUT);
  Serial.begin(9600);
 
  pinMode(9, OUTPUT);
}
 
void loop()
{
  PhotoSensor = analogRead(A0);
  // read the value from sensor
  Serial.println(PhotoSensor);
  // start a serial connection
  analogWrite(9, map(PhotoSensor, 0, 1023, 255, 0));
  // write analog value to pin 9
  delay(100); // Wait for 100 millisecond(s)
  // wait for 100 milliseconds
}

 

Click THIS to download LDR Code
Figure 5: Screenshot of Serial Monitor during LDR Simulation


2. OUTPUT Devices

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

A light-emitting diode (LED) is a semiconductor light source that glows when current passes through it. It is commonly found in lamps, street lights and etc due to its high efficiency. Next, let's set up a circuit on LEDs lights to let it blink.

Figure 6: Set up of RGB LEDs Circuit on Tinkercad


Simulation of RGD LEDs Circuit on Tinkercad
Click HERE for the simulation of RGB LEDs on Tinkercad

As seen from the circuit, the red wire is connected to a 5V pin which provides power. The black wire is connected to the ground. After which, 3 resistors with 220โ„ฆ are set up on the breadboard with one wire is connected the ground. Then, 3 LEDs with the colour in RED, YELLOW and GREEN are connected to the output pins wires and resistors respectively. PIN 13, 12 and 11 are declared as the output pins.

Programming needs to be set up to allow the circuit to work. The coding and explanation is reflected in the table below.

LEDs Coding

int animationSpeed = 0;
 
void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
}
 
void loop()
{
  animationSpeed = 400;
  digitalWrite(LED_BUILTIN, HIGH);
  delay(animationSpeed); // Wait for animationSpeed millisecond(s)
  digitalWrite(LED_BUILTIN, LOW);
  delay(animationSpeed); // Wait for animationSpeed millisecond(s)
  digitalWrite(12, HIGH);
  delay(animationSpeed); // Wait for animationSpeed millisecond(s)
  digitalWrite(12, LOW);
  delay(animationSpeed); // Wait for animationSpeed millisecond(s)
  digitalWrite(11, HIGH);
  delay(animationSpeed); // Wait for animationSpeed millisecond(s)
  digitalWrite(11, LOW);
  delay(animationSpeed); // Wait for animationSpeed millisecond(s)
}

 

 

Click THIS to download the LEDs code


b)  Interface the DC motor to maker UNO board and program it to on and off using a push button on the board.

A direct current (DC) motor is a type of electrical machine that converts electrical energy into mechanical energy. Small DC motor can be found inside toys, tools and various household items. Next, let's set up the DC motor circuit with a push-button on Tinkercad.

Figure 7: Set up of DC Motor Circuit on Tinkercad


Simulation of DC Motor on Tinkercad
Click HERE for the simulation of DC Motor on Tinkercad

As seen from the circuit, the red wire is connected to a 5V pin which provides power. The black wire is connected to the ground. PIN 2 is declared as the input digital pin of the push button while PIN 13 is declared as the output pin of the DC Motor. The resistor connected to the pushbutton has 10kโ„ฆ as this gives the pin a weak ground connection so that the pin is never floating. When the button is pressed, DC Motor will spin and when the button is released, DC Motor will stop spinning.

Programming needs to be set up to allow the circuit to work. The coding and explanation is reflected in the table below.

DC Motor Coding


int
buttonState = 0;
 
void setup()
{
  pinMode(2, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}
 
void loop()
{
  buttonState = digitalRead(2);
  // read the state of pushbutton
  if (buttonState == HIGH) {
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    digitalWrite(LED_BUILTIN, LOW);
  }
  // check if the pushbutton is pressed. If it is,
  // HIGH
  delay(10); // Delay a little bit to improve simulation performance
}

 

 

 Click THIS to download DC Motor Code



Practical Session

Moving forward, I was tasked to apply what I had learnt for Arduino Programming during the practical session. My team was given the challenge to make the Unicorn Wing Flap. The unicorn is familiar to us because we had done it last semester for the cardboard practical. However, there was an upgrade to that because we were tasked to programme it using Arduino Maker UNO Kit to make the wing flap. Our team decided to make the wing flap 10 times when the button is pressed. Below shows the end-product from the practical session.

Figure 8: End-Product of Unicorn


Figure 9: Servo Hidden Inside the Unicorn

Figure 10: The hole where Servo Wire Passes Through

Figure 11: Maker UNO Hidden in the Platform


Video 2: Mechanism of Servo In the Unicorn
Video 3: Unicorn Wing Flapping

Click THIS to download the coding for the Winged Unicorn Challenge

During the practical, my team finished doing the challenge early so the instructor challenged my team to make the wing and tail move during servo. Not only that but we are also challenged to make the eye of the unicorn light up using LED components. However, we did not manage to finish it during the practical due to time constraints so we decided to find a time during break time to discuss the challenge. 

The setting up of the circuit went smoothly as it is very straightforward. The problem we met was the programming part. When we tried programming, only one servo moves which gave us headache for a while๐Ÿ˜•. However, after trying for many times, we thought that we should introduce another statement to create servo for the second servo. And YES! IT WORKED!!! We were really glad as we have been trying for very long. Another problem met was initially we didn't manage to make the LED light to light up together with the servo in action. How we solved is making PIN 13 the output devices and then when the voltage is high, LED will light up and when the voltage is low, LED will stop lighting up. To put the LED at the eye of the unicorn, crocodile clips were used to connect the connection.

CLICK HERE to download the code and explanation for the NEW CHALLENGE!

Below shows the process & end-product of the challenge on UNICORN.

Figure 12: End-Product of New Challenge on Unicorn

Figure 13: Setting up the Circuit

Figure 14: Hiding the wires and MAKER UNO in the Platform

Unicorn in Operation


Reflection

For the individual task on input and output devices, I interfaced the potentiometer, LDR, LED and DC motor on Arduino board using Tinkercad. I think it was a fun experience using the Tinkercad as it is my first time exploring this website. It was not easy doing the individual task because I am not familiar with Tinkercad. Nonetheless, I followed the tutorial guides on YouTube to help me finish my work. After using Tinkercad to simulate the circuit, I think it is a very good website that aid in understanding the wires connected in a simpler way. Along the way, there was some frustration but I still managed to finish it by taking a rest and continuing it the next day.

As for the practical session in school, it was very fun and fruitful to have made the wing of the unicorn flapped using Arduino Programming. It helps me understand the working principle of some toys I had played with in childhood and it is quite fascinating. My team made use of Arduino Programming and servo to make the wing flap. We did well in this so the lecturer gave us a challenge to further apply what we had learnt. So, we were supposed to make the tail and eye lit also. We had a lot of concerns along the way. Even though the circuit works, we discussed how we can hide the wires and how we can put the LED light at the eye of the unicorn so that it looks like it is actually a legit toy. In the end, we came to a solution that we should make a platform beneath the unicorn to hide the components and wires used so that it looks more appealing. I think it was fun and it made us to think outside the box by using other materials to make it possible. For instance, we used crocodile clips wires to help us with the connection of LED to the eyes of the unicorn. I think this challenge has made the practical more fun and exciting. 

In conclusion, I think Arduino Programming is a very useful tool to embark on my programming journey. I used to think that I will never be able to do programming as I don't understand the language but after I went through this practical, I think I can do it with more practice and learning. I hope that I will take time to learn more about Arduino Programming and I will do it during the term break. I think Arduino Programming will be useful in my final year project and also to help my team with our chemical product air-to-water machine. Hopefully, I can master Arduino Programming during my free time.


































Comments

Popular posts from this blog

HOME (ABOUT ME & TEAM)

ABOUT ME! Welcome to MY BLOG!๐Ÿ“” (Chemical Product Design, 2021) Hello, I am Nander from DCHE2B01. As the new semester approaches, I will be taking up new modules for my chemical engineering learning journey. This semester, an interesting module called Chemical Product Design & Development will be taught and I'm more than happy to learn this module. To give a little preview, this module will involve designing and prototyping skills. In particular, I will be learning how to use Computer-Aided Design (CAD)  using Fusion 360, Laser Cutting, 3D Printing and Embedded Programming. Sounds INTERESTING right? As part of this learning journey, I'll be blogging to share what I learnt from this module. Hope you enjoy the blogs!  I'll be updating it weekly till the end of this semester. I hope to... As part of the learning, I hope I can improve my skills in using the techniques and software that will be introduced in this module. Below lists two personal goals that I wish to achieve