Arduino Programming

Today’s lesson is all about Arduino Programming. After the tutorial, we are tasked to do the pre-practical tasks as well as own individual tasks, which is using a website called TinkerCad to simulate the programming.

What is TinkerCad?


TinkerCad is a web-based simulation to develop and test Arduino code before we execute the code on the Arduino board.


As you can see, the picture below shows the home page of the TinkerCad.




Activity Time:


Here are the individual tasks we are going to do:



Activity 1 Part A (Input Devices):

1. First, I dragged the UNO board from the menu, as well as potentiometer anolog input.


2. In order not to confuse about the wires, I changed the colour of the wires (Red, Black and Green) so that I could easily identify. The left pin of the potentiometer was connected to the power of 5V, the middle pin was connected to GND, and followed by the right pin which was connected to anolog A0.



3. Picture below showed the code for the setup. I would need to discard the default code, as you could see there was a bin icon, so I was able to drag the default code into the bin easily. Hence, I needed to create a code in order to achieve the goal for this activity.


4. Select CREATE VARIABLE..., then type in the keyword SensorValue, which I had learnt during the tutorial into the bracket.


5. What should you do next? Firstly, I dragged and put the 2nd block, which was located below the SensorValue block as the first row. Replace '0' in the set block with the read analog pin A0. For the second row, I chose Output (blue icon), select PRINT TO SERIAL MONITOR, change the variable to SensorValue once again, and you were all set!



6. At first, I was so frustrated about the needle because it did not move automatically after the code had been sent for simulation. After I watched the TinderCad tutorial videos for information, I realised that I would need to move the needle by myself after the instructor mentioned about it. And yeah, that's the whole process for part 1 activity, and below is the video of the simulation.





Activity 1 Part B:

1. For this part, my goal was to allow the LDR to be able to measure the signal in serial monitor Arduino IDE. First, I would need an UNO Board, photoresistor LDR, a resistor to control the current to flow through, as well as the Breadboard. Breadboard played an essential role because I would need it to built and test the circuits before sent it for simulation.



2. I needed to ensure that the wire connected to the power pin would have to connect to the negative terminal of the breadboard, and another wire which connected to the GND pin should have to connect to the + terminal of the breadboard.



3. Then, I put the photoresistor LDR on the breadboard wherever I preferred, followed by inserted the resistor. I noticed that there were 2 pins for the LDR, whereas photoresistor had 3 pins, so I would need to pay attention for that.



4. The thing that I must remember when doing the simulation was that after I connected the wire from the UNO Board to the terminals of the breadboard, I would also had to connected the wire to the both end of the resistor as well as the photoresistor as shown in the picture.


5. Next is the coding. Basically, the code that I used was actually the same as the code used for the last part of the activity, just ensured the code entered was verified before sent it to the simulation.


6. Below is the video demonstration for this part B activity.



Activity 2 Part A (Output Devices):


1. Next to carry on for the output devices. For this task, it was pretty similar to the individual competency test that I had taken during the practical, which was light up the LED with time interval. Basically, what I needed were 1 UNO maker, 3 different colour of LED (Green, Yellow and Red), and resistors.


2. Secondly, I attached the 3 LED on the breadboard in the position that I preferred.



3. After that, I attached the resistors in the end of the resistors, and for the other end, I would need to connect with the wires. Be mindful that there was a gap on the breadboard where I attached the resistors.



4. What I did on my next step was I attached the end of the wire to GND pin, and attached the other end to the negative terminal (only) row of the breadboard.



5. Here was the setup for this activity. What I did was I attached the yellow wire, which was for yellow LED to pin 12, then green wire for green LED to pin 11, followed by red wire for red LED to pin 10. Do note that the other end of the wire connected need to be attached to the end of each resistors.



6. Next was the difficult part, coding. And yes, the hardest and the most headache part! First, we needed to indicate that LED had been connected to the Arduino, and we needed to indicate in our coding so that the Arduino will take note of it. So, I put the indication(highlighted parts) to the first few rows in the code.



7. We can carry on to let the Arduino acknowledge that all the LED that had been attached were acted as output devices. Therefore, I would need to indicate the output devices in each LED, by using:


pinMode (colour of LED, output).


8. For the final step, I would need to think of those LED needed to have the same function as the traffic light, which lighted up in a time interval. For this case, I would need to use VOID LOOP, follow by DIGITALWRITE (Colour of LED, HIGH/LOW), followed by the time delay, Here I set 1000 ms for my time delay. For the HIGH/LOW, it meant that HIGH was for the LED to light up, and LOW was for the LED to not light up. After I had done the yellow LED, remembered to do for the red LED and green LED too!!!



9. REMEMBER to attached another wire to the cathode of each LED and attached the other end to the + terminal of the breadboard, so that the circuit would become complete and LED would be able to light up!




10. Here is the video demonstration for this activity, ENJOY!!!




Activity 2 Part B:


Finally, we are down to 1 last part! So this final activity needs us to interface a DC motor to UNO board, and also using push button to on and off to activate the DC Motor.

1. For this activity, I need breadboard, UNO board, a DC motor, a push button and a resistor.



2. I would have to build a circuit using breadboard once again. First, I attached the wire to the GND pin, and attached the other end of the wire to the negative terminal of the row of the breadboard.


3. Since all the connection were finished assembled, the circuit for DC motor had been built up successfully done! I used different colour of wires to make sure myself I did not confuse myself.




4. Now we came to the coding part! I indicated the position of the Button and the DC Motor to the Arduino, so that Arduino would help me to take note. I indicated the position of the button which located at pin 13, whereas the DC motor was located at pin 7.



5. For the next step, we needed to make the Arduino to help us to remember the input and output for the circuit itself. When the button is pushed down, the DC motor would rotate, therefore we put the button as input and DC motor as output. What we did was I used VOID SETUP, and indicate pinMODE (Button/Motor , INPUT/OUTPUT).


6. When button was pressed, the DC motor would rotate. We put 1 when one pressed the button. If no one pressed down the button, the motor would not rotate, therefore we put 0. You were all set, and have fun with the simulation!!!




7. Here is the video demonstration of the circuit during simulation.




Individual Tasks DONE!!!


Arduino Code Resources:

Activity 1 Part A and B:

// C++ code
//
int SensorValue = 0;

void setup()
{
  pinMode(A0, INPUT);
  Serial.begin(9600);

}

void loop()
{
  SensorValue = analogRead(A0);
  Serial.println(SensorValue);
  delay(10); // Delay a little bit to improve simulation performance
}


Activity 2 Part A:

// C++ code
//

const int yellowLED = 12;
const int greenLED = 11;
const int redLED = 10;

void setup ()
{
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
}

void loop ()
{
  digitalWrite (yellowLED, HIGH);
  delay(1000);
  digitalWrite (yellowLED, LOW);
  delay(0);
  digitalWrite (greenLED, HIGH);
  delay(1000);  
  digitalWrite (greenLED, LOW);
  delay(0);
  digitalWrite (redLED, HIGH);
  delay(1000);
  digitalWrite (redLED, LOW);
  delay(0);
}


Activity 2 Part B:

int Button = 13;
int Motor = 7;

void setup()
{
  pinMode (Button, INPUT);
  pinMode (Motor, OUTPUT);
}

void loop ()
{
  int buttonState = digitalRead (Button);
    
    if (buttonState == 1){
    
    digitalWrite (Motor, LOW);
    
    } else {
    
    digitalWrite (Motor, HIGH);
    
  }

}


Reflection:

I used to think that Arduino programming is tough for me, as I do not have the background in programming. Besides that, I always struggling in coding. Therefore, what I do is I am able to approach my groupmates, and classmates from other classes, or even our own seniors for help and they are willing to help me by guiding me on how to do coding properly. Now I think that I have some basic understanding on how to do the coding stuff. So next I will improve myself more especially on coding part in Arduino Programming in order to prepare for my future project if applicable.
































































































No comments:

Post a Comment