Arduino Programming (Tinkercad Tasks)

 

For this week, I tried to use the site Tinkercad to practice Arduino.

For all of the tasks, I started with a breadboard that was already in the Tinkercard where the breadboard positive and negative sides are connected to the 5V and ground(GND) of the board, respectively.

1       Input devices:

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

The code I used:

Modified code

Example code from Tinkercad

// C++ code

//

int SensorVal = 0;

 

int Light = 0;

 

void setup()

{

  pinMode(A1, INPUT);

  pinMode(10, OUTPUT);

  Serial.begin(9600);

 

}

 

void loop()

{

  // Read the value from the sensor

  SensorVal = analogRead(A1);

  SensorVal = map(SensorVal, 0, 1023, 0, 180);

  // Turn LED ON

  analogWrite(10, Light);

  digitalWrite(10, HIGH);

  // print out the signal value:

  Serial.print("sensor=");

  Serial.println(SensorVal);

  // pause for <SensorVal> milliseconds

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

  // Turn LED OFF

  analogWrite(10, Light);

  digitalWrite(10, LOW);

  // print out the signal value:

  Serial.print("Light=");

  Serial.println(Light);

  // pause for <Light> milliseconds

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

}

// C++ code

//

/*

    Analog Input  Demonstrates analog input by

  reading an analog sensor on analog pin 0 and

  turning on and off a light emitting diode(LED)

  connected to digital pin 13.  The amount of time

  the LED will be on and off depends on  the value

  obtained by analogRead().

 

  The circuit:

  * Potentiometer attached to analog input 0

  * center pin of the potentiometer to the analog

  pin

  * one side pin (either one) to ground

  * the other side pin to +5V

  * LED anode (long leg) attached to digital

  output 13

  * LED cathode (short leg) attached to ground

  * Note: because most Arduinos have a built-in

  LED attached  to pin 13 on the board, the LED is

  optional.

 

  Created by David Cuartielles

  modified 30 Aug 2011  By Tom Igoe

 

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogInput

*/

 

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

  // stop the program for the <sensorValue>

  // milliseconds

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

  // turn the LED off

  digitalWrite(LED_BUILTIN, LOW);

  // stop the program for the <sensorValue>

  // milliseconds

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

}

 

When I started with this task, I was very unsure of what to do as the interface of Tinkercad was very new to me. As I started exploring, the tutorials, they “blocks” of code were also very different from what I was used to. After playing around with the site for a while, I found the option of the “text” which made things much easier.


For the Potentiometer, I first tried to find the component in the options in the Tinkercad with the most similar setup for my uses which yielded the example code above. After that, I added a breadboard which made it a lot easier for me to see the intersecting wires and attach the Multimeter which is the component that measures the signal.

For the 3 pins of the Potentiometer, I attached the terminals 1 and 2 to power and ground, respectively and attached a wire from the “wiper” to A1 (yellow wire).

To make the print easier to read I added the line “sensor = “.

The set up was a bit hard to see what was going on without the code so I added an LED as a sort of signal to tell me when there was current.

To make the print easier to read I added the line “light = “.

 HERE is the downloadable code!


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

My code

// C++ code

//

int Photosensor = 0;

 

void setup()

{

  pinMode(A0, INPUT);

  Serial.begin(9600);

 

  pinMode(9, OUTPUT);

}

 

void loop()

{

  // Read the signal from the LDR

  Photosensor = analogRead(A0);

  Serial.println(Photosensor);

  // Start a serial connection

  analogWrite(9, map(Photosensor, 0, 1023, 0, 255));

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

}

 

For this task, I started off with a breadboard, an LED, a resistor at 220 ohms and connected them so that the LED is connected to pin 9. After that, I added the LDR to the breadboard and attached the positive end to pin A0 and the negative end to GND. Then a 4700 ohm resistor was used to attach the negative side of the LDR to the breadboard.


I tried using the blocks for the code of this which was actually quite similar to the text when I got used to it. I set the Photosensor to read A0 and then set pin 9 (which is connected to the LED) to read that so that the LED would dim and brighten according to the LDR.

LED works from 0 to 255 so to make the LED light up when its dark, I made it so that the side which is connected to the A0 is connected the to positive side so that  when there is no light, LDR resistance is high, the LED will be ON.


HERE is the downloadable code!

 

2 Output devices:

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

Modified Code

Example Code

// C++ code

//

int animationSpeed = 0;

 

int Brightness = 0;

 

int j = 0;

 

int i = 0;

 

void setup()

{

  pinMode(11, OUTPUT);

  pinMode(10, OUTPUT);

  pinMode(9, OUTPUT);

}

 

void loop()

{

  animationSpeed = 3;

  for (Brightness = 0; Brightness <= 255; Brightness += 1) {

    analogWrite(11, Brightness);

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

  }

  for (Brightness = 255; Brightness <= 0; Brightness += 1) {

    analogWrite(11, Brightness);

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

  }

  for (Brightness = 0; Brightness <= 255; Brightness += 1) {

    analogWrite(10, Brightness);

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

  }

  for (Brightness = 255; Brightness <= 0; Brightness += 1) {

    analogWrite(10, Brightness);

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

  }

  for (Brightness = 0; Brightness <= 255; Brightness += 1) {

    analogWrite(9, Brightness);

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

  }

  for (Brightness = 255; Brightness <= 0; Brightness += 1) {

    analogWrite(9, Brightness);

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

  }

}

// C++ code

//

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)

}

 

For this task, I started with the set up in Tinkercard with 2 LEDs but the code was to make the turn OFF and ON in turns so to make it my own, I wanted to lights to fade in and out because they remind me of Christmas lights and its December soon.

The problem with making the LEDs fade was that they were attached to pins 13 and 12 which only have HIGH or LOW so I shifted the pins there were attached to 11-9. (in the picture below)
It was at this point I realized a pattern in connecting LEDs which really helped me in later parts where it was output devices and it was much easier to connect because I understood the connections better.
To make them fade in and out, I used the “count up” and “count down” function in block or the “for” command in text. I set the variables as “Brightness” (which were from 0 to 255) as well as “animationSpeed" (which was set to 3 ms). I had to move the  pins for the LEDs since they were not able to give me varying brightness for the LEDs. The "animationSpeed" helped me to change the timing easily.



HERE is the downloadable code!


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

Finalized Code

// C++ code

//

int buttonstate = 0;

 

void setup()

{

  pinMode(2, INPUT);

  pinMode(LED_BUILTIN, OUTPUT);

  pinMode(9, OUTPUT);

}

 

void loop()

{

  // Read state of pushbutton

  buttonstate = digitalRead(2);

  // Check is pushbutton is pressed

  if (buttonstate == HIGH) {

    // If button is pressed, turn LED ON and DC motor

    // ON

    digitalWrite(LED_BUILTIN, HIGH);

    digitalWrite(9, HIGH);

  } else {

    // If button is NOT pressed, turn LED OFF and DC

    // motor OFF

    digitalWrite(LED_BUILTIN, LOW);

    digitalWrite(9, LOW);

  }

  delay(10); // Delay a little bit to improve simulation performance

}

 

For this, I followed the instruction video on how to use a push button and adapted that for this task.


I used a 220ohm resistor for the LED, 10kohm resistor for the button and a 1kohm resistor for the DC motor. I don’t think that the LED was part of the task but it is a way indicate that the motor is running.


I attached from the example code on how to make a button pressing LED, I just added the DC motor to pin 9 along with a resistor.


HERE is the downloadable code!


Highlights of Learning Points

Arduino is a lot less daunting than what I thought when I started. When I realized the pattern of attaching the power and ground, attaching pins and doing the codes, the later tasks became much easier.

There are many sources where I can learn. At first, I was going back and forth with the slides but I realized I am more of an audio learner so youtube videos and resources as well as asking my friends was much more useful than reading the slides which just didn’t stick for me. Which leads to my last and most important thing I learnt:

Doing is the easiest way to gain the confidence to learn sometimes. Making mistakes such as connecting wrongly and missing resistors made it easier to make mistakes. Correcting myself and learning how to do it right without a person guiding me the whole way, I am more confident than when a teacher tells me what to do. The fact that tinkercad is virtual also meant that I would not damage the components which made it a lot easier to do trial and error in learning.



No comments:

Post a Comment

Welcome to my blog!

 This is the Blog of Chyrvel Gwyn Nagamos for the module CP5070. Feel free to explore all of my projects and learning from the module!