Arduino Programming
Hi, I am required to complete 4 challenges for Arduino.
1A.Interface a Potentiometer Analog Input to maker UNO board and measure its signal in serial monitor Arduino IDE.
This is my design of the Potentiometer Analog Input.
I decided to use a potentiometer with a LED so that it is able to show the potentiometer controlling the LED brightness.
This is my code:
int sensorValue = 0;
void setup()
{
pinMode(A0,INPUT);
pinMode(13,OUTPUT);
}
void loop()
{
//read the value from the sensor
sensorValue = analogRead(A0);
//turn the LED on
digitalWrite(13,HIGH);
//pause the program for <sensorValue> milliseconds
delay(sensorValue); //Wait for sensorValue milliseconds
digitalWrite(13,LOW);
//pause the program for <sensorValue> milliseconds
delay(sensorValue); //Wait for sensorValue milliseconds
}
For this code, I used (int sensorValue) used to program and address analog pins on the Arduino board and will also return the analog input reading which is between 0 to 1023. After that I used pinMode for my input and output for A0 and 13 and used a voidloop for the sensorValue to be the analogRead(A0) and turn the LED on or off depending on its resistor based on digitalWrite(13,HIGH), (13,LOW). I added a delay in between to see the difference.
Video of my code working: https://drive.google.com/file/d/1G-IRzULj_g1bxC8EDQMMdAhq2_TY3110/view?usp=sharing
1B. Interface an LDR to maker UNO board and measure its signal in serial monitor Arduino IDE.
This is my design of the LDR to makerUNO board with a LED.
My code:
int sensorValue = 0;
void setup()
{
pinMode(A0,INPUT);
pinMode(9,OUTPUT);
Serial.begin(9600);
}
void loop()
{
// read the value from the sensor
sensorValue = analogRead(A0);
// print the sensor reading so you know its range
Serial.println(sensorValue);
// map the sensor reading to a range for the LED
analogWrite(9, map(sensorValue, 0, 1023, 0, 255));
delay(100); // Wait for 100 millisecond(s)
}
For my code, I used Serial.begin(9600) which is a command you give to the Arduino to begin serial communication. For my void loop, I used sensorValue = analogRead(A0) to read the value from the sensor and Serial.printIn(sensorValue); to print the sensor reading. analogWrite() takes two arguments, the pin number (9 in our case), and the value to write, which should be between 0 and 255. The inline function map() takes five arguments: the number to evaluate (the ever-changing sensor variable), the expected minimum and expected maximum, and the desired min and max. So the map() function in our case is evaluating the incoming sensorValue, and doing some cross multiplication to scale the output down from 0-1023 to 0-255. The result is returned into the second argument of analogWrite(), setting the brightness of the LED connected to pin 9.
Video of the code working: https://drive.google.com/file/d/1KTcL6gaKNmz2YGjlCx84_z3SSMEZ-CQc/view?usp=sharing
2a. Interface 3 LEDS (Red, Yellow, Green) to Maker UNO board and program it to perform something (fade or flash etc)
Here is my design of 3 LEDS flashing.

I decided to make the 3 LEDS flash for 100 milliseconds.
This is my code:
void setup()
{
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
}
void loop()
{
// turn the LED on (HIGH is the voltage level)
digitalWrite(13, HIGH);
delay(100); // Wait for 1000 millisecond(s)
// turn the LED off by making the voltage LOW
digitalWrite(13, LOW);
delay(100); // Wait for 1000 millisecond(s)
digitalWrite(11, HIGH);
delay(100); // Wait for 1000 millisecond(s)
// turn the LED off by making the voltage LOW
digitalWrite(11, LOW);
delay(100); // Wait for 1000 millisecond(s)
digitalWrite(9, HIGH);
delay(100); // Wait for 1000 millisecond(s)
// turn the LED off by making the voltage LOW
digitalWrite(9, LOW);
delay(100); // Wait for 1000 millisecond(s)
}
Here is a video of my code working: https://drive.google.com/file/d/1qHerrcftL7il_--PS2tGgx6BDa2Z-9Gp/view?usp=sharing
2B. Interface a DC Motor to Maker UNO Board and program it to on and off using push button on the board.
// the setup function runs once when you press reset or power the board
void setup() {
pinMode(3, INPUT);
digitalWrite(3, HIGH);
pinMode(5, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
if (digitalRead(3) == 0)
digitalWrite(5, HIGH);
else
digitalWrite(5, LOW);
}
This code allows me to control the output and input High and Low and a condition function to help me to turn it off and on. I used a transistor as a switch to help make the coding easier.
Video of my code working: https://drive.google.com/file/d/1y5arX-33HdwM23QPsX1OJKyv7RCKfc6L/view?usp=sharing
Here are the url links for the different activities:
Reflection: I was proud of myself for completing the 4 activities and learning a new Arduino website called Tinkercad. There were some problems that I faced which were using Tinkercad and solving the last activity which is the dc motor and button. I managed to learn Tinkercad by following step by step to a youtube video and managed to understand Tinkercad. I solved the last activity by researching online and learning from builds of dc motor and button. I also learnt a new component called a transistor to help me with the last activity. For input devices to arduino board, I learn that it is mostly the same to set the component such as LED or dc motor to output for the pinMode with the allocated pin number and for output devices to arduino board such as LDR or potentiometer requires changing the pinMode to input with the allocated pin number. Sensors are mostly input devices. Overall, I'm happy with my progress of learning Arduino and its new components.



Comments
Post a Comment