Arduino programming

Arduino Programming is a beginner’s introduction to the world of coding technology. πŸ“²πŸ–§πŸ‘Ύ

With Arduino, I am able to automate a system with its program function.

For example, I can move a servo motor unit, play music or even light up an LED. Furthermore, these are all just BASIC examples of what coding can achieve.


When practised by professionals, I'm sure that coding is the right step towards a future with science-fiction technology that are depicted in comics.

For beginners, coding is made easy with the help of Arduino Maker UNO and the amazing online software that I used as its counterpart, Autodesk Tinkercad.

Basis of all connections to the Breadboard

There will be 4 tasks explained subsequently in this page:

  1. Input devices:

  1. Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE.

  2. Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE

  1. Output devices:

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

  2. Include the pushbutton on the MakerUno board to start/stop part 2.a. above

For each of the tasks, I will describe:

  1. The program/code that I have used and explanation of the code. The code is in writable format (not an image).

  2. The sources/references that I used to write the code/program.

  3. The problems I encountered and how I fixed them.

  4. The evidence that the code/program worked in the form of video of the executed program/code.

Finally, I will describe:

  1. My Learning reflection on the overall Arduino programming activities.


Input devices: Interface a potentiometer analog input to maker UNO board and measure/show its signal in serial monitor Arduino IDE.

  1. Below are the code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

// C++ code

//

int sensorValue = 0;

 

void setup()

{

  pinMode(A0, INPUT);

  pinMode(10, OUTPUT);

  Serial.begin(9600);

}

 

void loop()

{

  // read the value from the sensor

  sensorValue = analogRead(A0);

  // turn the LED on

  digitalWrite(10, HIGH);

  // pause the program for <sensorValue> milliseconds

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

  // turn the LED off

  digitalWrite(10, LOW);

  // pause the program for <sensorValue> milliseconds

  delay(sensorValue); 

// Wait for sensorValue millisecond(s)

  

//print the value of the SensorValue

Serial.print("sensorValue:");

  Serial.print("");

  Serial.println(sensorValue);

}

  • Analog pin A0 is set as the input pin.


  • A potentiometer is used to vary the resistance signal supply for the LED output connected to pin - 10


  • The resistance signal can be measured and shown on the serial monitor via the program’s established serial communication between the  Arduino board and the serial monitor device.

  1. Below are the hyperlink to the sources/references that I used to write the code/program.


            Below are the problems I have encountered and how I fixed them.

Problems encountered

How I fixed them

I could not get the LED light to blink to show that my potentiometer was working to change the resistance, despite the other devices working normally.

  • Initially, I thought that it may be an improper connection issue

  • However, after trying out the connections again (e.g. change resistor position, change LED position), there was no success

  • I went to Autodesk’s youtube channel

  • Watched their video on how to blink an LED

  • Managed to connect the negatives and positives to channel power supply to my LED

  • I learned that as long as the components of same polarity are connected vertically and different polarity are separated horizontally, they will connect, since the cross-section of the breadboard are as follows (vertical strips are interconnected, horizontal strips are separated)

I could not get the serial monitor to display my workings

  • I went on to google how to get the serial monitor to display signal readings

  • I realised I needed to establish communication between serial monitor and the board for the monitor to display my readings

  • I input a new string of codes to test whether the serial monitor will display my readings

  • It finally managed to work


  1. Below is the short video as the evidence that the code/program work.

The serial monitor reads the changes to its received signal when the potentiometer notch is turned.


Input devices: Interface a LDR to maker UNO board and measure/show its signal in serial monitor Arduino IDE:

  1. Below are the code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

// C++ code

//

/*

  Enables an LED to light up in the absence of

  light and dim in the presence of light

*/

 

int photosensor = 0;

 

void setup()

{

  pinMode(A0, INPUT);

  Serial.begin(9600);

  pinMode(10, OUTPUT);

}

 

void loop()

{

  // read value from sensor

  photosensor = analogRead(A0);

  // establish communication btwn serial monitor and

  // board

  Serial.println(photosensor);

  // LEDs work between the range of 0-255

  analogWrite(10, map(photosensor, 0, 1023, 0, 255));

  // wait for 100ms before repeating the loop

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

}


  • Enables an LED to brighten in the absence of light and dim in the presence of light


  • Harnesses the ability of the photoresistor for this code to work


  • The signal is measured by serial monitor whose communication is established via connection between serial monitor and Arduino device


  • variable int; integer is created as a new variable in the code sequence



  1. Below is the hyperlink to the sources/references that I used to write the code/program.


  1. Below are the problems I have encountered and how I fixed them.

  • My photoresistor initially brightened in the presence of light which was rather redundant

  • I had 2 options, to reprogram my code, or to rewire the wires along the breadboard

  • Of course, I chose the latter which was much easier

  • Hence, I rewired my connections and included a 10000-ohm resistor for the photoresistor


  1. Below is the short video as the evidence that the code/program work.



The video depicts the serial monitor rapidly varied signal values due to the changes in brightness sensed by the photoresistor. Really interesting display to watch!

LEDS work from 0-255 where 255 is the maximum possible value for rgb

This concept relates back to the first practical on laser cutting, where we had to edit the rgb values.


As shown in the videos, the LED varies its brightness based on the level of light detected from its immediate surroundings.





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

  1. Below are the code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

// C++ code

//

int animationSpeed = 0;

 

void setup()

{

  pinMode(13, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(11, OUTPUT);

}

 

void loop()

{

  animationSpeed = 400;

  digitalWrite(13, HIGH);

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

  digitalWrite(13, 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);

}


Triple LED Light-up Display Flash and Fade


Similar to a Traffic Light system, the code enables the LED display to light up in a sequence from green, yellow then red.



  1. Below are the hyperlink to the sources/references that I used to write the code/program.

https://www.youtube.com/watch?v=MojSo7OtF9w 



  1. Below are the problems I have encountered and how I fixed them.

Problem encountered

How I fixed them

The LEDs were all so cramped up on my breadboard initially

I decided to then space them apart so that I could have a better working field

On my digital site, my LED only turned on all at once 1, 2, 3 then they continued to be left on without turning off.


This did not have a sense of flashiness to it at all

I troubleshooted my code block builder and realised I did not include the signal “LOW” function to turn off my LED pin output.


Hence, I added new strings of blocks to turn off the LEDs and turned my breadboard setup into something out of Christmas Holiday or also a traffic light setup.



  1. Below is the short video as the evidence that the code/program work.



Output devices: Include pushbutton to start/stop the previous task 

  1. Below are the code/program I have used and the explanation of the code.

Code/program in writeable format

Explanation of the code

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);

  pinMode(12, OUTPUT);

  pinMode(11, 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(5, LOW);

  } else {

    for (int i=0; i< 5; i++)

    {

    digitalWrite(13, HIGH);

    delay(500);

    digitalWrite (13, LOW);

    delay(500);

    digitalWrite(12, HIGH);

    delay(500);

    digitalWrite (12, LOW);

    delay(500);

    digitalWrite(11, HIGH);

    delay(500);

    digitalWrite (11, LOW);

    delay(500);

  }

}

}

 


Begins a light show sequence of 3 fading lights when the button on Arduino Board is pressed.


Button on the board is programmed to allow a code to be played when it is pressed.





  1. Below are the hyperlink to the sources/references that I used to write the code/program.


  1. Below are the problems I have encountered and how I fixed them.

I was really stuck on this one and did not know where to start. I attempted Youtube and the reliable Autodesk channel was of little help.

I then recalled that I did up a Programmable button LED challenge for one of the introductory pre-practical activities.


I hastily opened up the document where my code was and I pasted it into a new sketch on my IDE application.


I then added 2 other output signal holders into my code into the setup and then along with the loop. I checked the code and uploaded it to my board.


At first, I made the connection of the output signals and only saw that the board's pins were lighting up; my Breadboard LEDs were off.


I then connected the rest of the wires to their relevant connections and hoped for the best...


Eureka! It finally worked. I was amazed at my quick thinking and recalling of information when I needed it.


  1. Below is the short video as the evidence that the code/program work.





Below is my Learning Reflection on the overall Arduino Programming activities.

Overall, the Arduino Programming activities were really tough and frustrating for me and gave me a hard time. I had many problems and troubleshooting obstacles. πŸƒ


However, I persevered and managed to complete all of them proudly with the assistance of, and honourable mentions to Youtube and Google πŸ…πŸ₯‡.


Also, the medium that made my blog activities possible was Autodesk Tinkercad. I could create code strings easily with their block builder function and the translated code would be there for me to download and open with my Arduino IDE application. This was really useful for me as a novice to coding. For the physical connections, I could simply copy what was done already in Tinkercad and test/troubleshoot my softcopy design before physically making wire connections. I felt like a surgeon or seamstress, delicately placing the wires into the breadboard.


For the breadboard connections, before I had watched any videos, I had completely no understanding of how it worked and I thought that I had to push the wires through the holes and go into the holes and come out of them to the opposite. However, now I know that the breadboard has metallic connections vertically and are not connected horizontally. This makes my breadboard wiring much more efficient.

If I were to redo breadboard connections or explain it to a newbie, I definitely could. I hope that I am able to apply this newfound skill into my final prototype project for this module, the tea maker machine.




Arduino and Breadboard wiring for Potentiometer


At a glance, any Tom, Dick or Harry would not be able to understand what in the world this is. However, after I went through the learning exercises and packages, I am able to easily explain this picture and circuit.


From this, I realised that anyone is able to do programming. However, it is a skill that requires painful amounts of time and dedication. If someone is uninterested in it, they will never be able to master this skill. Likewise, this applies to everything for life.


And as always, in order to learn a new skill, we want to start up slowly by attempting easy codes. We then slowly ramp up our engine to create tougher codes.


After completing this suite of activities, I think that high-level coding is not my thing. But, I could definitely look at creating new functional codes via the Autodesk Tinkercad website. I was really excited every time I created a new workable code, and thought to myself, "I'm a genius!" Furthermore, the Arduino Maker UNO kit comes with many kinds of device add-ons for me to play with.


Practical:

With respect to the core practical, my team distributed ourselves into pairs. This activity was interesting, where we were exposed to a live scenario of turning our codes into reality.

Hero Shot of my team and me with our final products

Hong Yi and I made the majestic Protector Pegasus, the Crimson Midnight Pegasus; Betty

Here's a video of her flapping her wings, together with the timely Christmas theme.


The majestic Betty being photographed up-close πŸŽπŸ΄πŸ‘€


Through and through with this Arduino coding suite, one thing remained constant, it was that all these activities taught us the skill of turning our Arduino codes into a useful application for real-world use. Although still a beginner, I can safely say that I am able to program my Arduino Maker circuit to perform the function that I want.


To end, I also managed to complete one of the personal goals 😝 that I had set for myself at the start of this module. I am now able to understand how coding is done, as that was something that I had always been curious about. Now, I am able to code my board circuit to do simple practical activities, such as turn on an LED when there is no light! πŸ’‘

Popular Posts

Image

About me