Bare Minimum
Background Knowledge:
About Arduino and the Arduino IDE
Introduction to Arduino with TinkerCAD
What you'll need:
1 - Arduino UNO
1 - Arduino USB Cable
Creating the bare minimum setup on an Arduino involves setting up the microcontroller to run without any external components except for power. This is useful for testing the basic functionality of the microcontroller and for situations where you have limited resources.
Connect the Arduino:
Plug your Arduino board into your computer using the USB cable.
Open the Arduino IDE:
Launch the Arduino Integrated Development Environment (IDE) on your computer.
Select the Board:
Go to Tools > Board and select the appropriate Arduino board you're using (e.g., Arduino Uno, Nano, etc.)
Select the Processor:
Go to Tools > Processor and select the specific microcontroller model (e.g., ATmega328P for Arduino Uno).
5. Select the Port:
Go to Tools > Port and select the COM port to which your Arduino is connected.
6. Choose Programmer (if necessary):
Go to Tools > Programmer and select the appropriate programmer if you're using an external programmer (not needed for most setups).
7. Write the Sketch:
Write a simple Arduino sketch to test the microcontroller. See this process below.
8. Upload the Sketch:
If using TinkerCAD, copy and paste the C++ code that you have either written or that was generated from the block coding feature into the Arduino IDE. Click the "Upload" button (right arrow icon) in the Arduino IDE to upload the sketch to the microcontroller.
Bare Minimum Coding
When creating a Bare Minimum Sketch, the absolute least you will need is an "On Start" and "Forever" Control Block on TinkerCAD. In C++, this will translate to the "void setup()" and "void loop()" commands.
void setup()
{
}
void loop()
{
delay(10); // Delay a little bit to improve simulation performance
}
Why Create the Bare Minimum Setup
Resource Constraints:
In some projects, you might have limited components or space, and setting up the microcontroller with only power might be necessary.
Testing Microcontroller Functionality:
It's a quick way to verify that the microcontroller itself is working. For example, if you're troubleshooting a project, you can eliminate external components as potential issues. This usually is paired with a command like blinking the onboard LED to have visual confirmation the board and sketch are working.
Important Note:
While this setup can be useful for certain scenarios, keep in mind that for most practical projects, you'll need additional components like resistors, capacitors, and other sensors or actuators. This bare minimum setup is primarily for testing and debugging the microcontroller itself.
Exercise One: Blink
Background Knowledge:
About Arduino and the Arduino IDE
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - 470 Ohm 1/4 Watt THR
1 - RED 5mm LED
1 - Jumper Wire Set
The "Blink" project is one of the most fundamental and introductory projects in Arduino programming. It involves turning an LED (Light Emitting Diode) on and off using an Arduino microcontroller. By writing a simple program, users learn how to control digital pins, set them as outputs, and use basic functions like digitalWrite() and delay(). This project serves as a starting point for beginners to get familiar with the Arduino IDE, hardware connections, and basic coding concepts, providing a solid foundation for more complex projects in the future.Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO it is attached to digital pin 13. LED_BUILTIN is set to the correct LED pin independent of which board is used.
Follow these steps to build the circuit:
Connect power and ground to the Breadboard power rails. Positive will come from pin 13, and ground can come from any of the three (3) GND pins on the Arduino board.
Insert the LED into the breadboard.
Note: The longer leg is the positive (anode) and the shorter leg is the negative (cathode).
Connect one leg of the resistor to the anode of the LED and the other to the positive rail of the breadboard.
Connect the cathode of the LED to the negative rail of the breadboard.
Plug your Arduino board into your computer using the USB cable.
Writing the Code
On TinkerCAD there are blue output blocks that allow you to set the Arduino pins to outputs for various devices. In this case, you will be setting the output to the built-in LED which is also tied to pin 13 on most Arduinos- because of this, you do not have to also set pin 13 as an output.
In your Forever Control Loop, place the "set built-in LED" block like a puzzle piece. This block should be set to HIGH which means the full available voltage the Arduino is able to output. Follow that with a wait command with 1 second or 1000 milliseconds. Repeat this process but this time setting your built-in LED to LOW which means zero voltage.
In TinkerCAD, the C++ code will be automatically generated from the blocks and can be copied and pasted into the Arduino IDE for Upload.
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(1000); // Wait for 1000 millisecond(s)
}
Questions:
What is the purpose of the resistor in the circuit?
How can you modify the sketch to make the LED blink faster or slower?
Can you add more LEDs to the circuit and control them separately?
Exercise Two: Brightness and Fade (PWM)
Background Knowledge:
How to read a Schematic
How to use a Breadboard
How to read and use a Multimeter
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - 470 Ohm 1/4 Watt THR
4 - RED 5mm LED
1 - Jumper Wire Set
What is PWM?
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between the full Vcc of the board 5V on the Uno board and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and Vcc controlling the brightness of the LED. In the graphic to the right, the red lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the red lines would measure 2 milliseconds each max. A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.
Steps
Build the circuit to the right on TinkerCAD.
Code the Arduino using TinkerCAD Block Coding
Keep in mind that you will be using PWM to LED1 will be 100% on, LED2 will be 50% on, and LED 3 will be 25% on. What does that code look like? What does the PWM need to be set to to accomplish this?
In TinkerCAD, setting an output block to a number between 0 and 255 will adjust the output between 0V and 5V. The example below is setting pin 3 to output 255 PWM which is 100% of Arduino's 5V output.
void loop()
{
analogWrite(3, 255);
delay(10); // Delay a little bit to improve simulation performance
}
Fill out the chart below to track this information.
Use the multimeter to measure the voltage output over the LED and Resistor.
4. Code LED4 so that it cycles through brightness.
Cycling brightness should start off at 0% brightness and works itself up to 100% and then back down to 0%. What does that code look like?
void loop()
{
for (PWM = 0; PWM <= 255; PWM += 1) {
analogWrite(3, PWM);
delay(10); // Wait for 10 millisecond(s)
}
}
In TinkerCAD, cycling up or down power using PWM can be done using the "Count" command and assigning a variable to the count and output.
5. Measure the Voltage over LED 4 and the Resistor in series with it.
What do you see happening?
What is the average voltage the multimeter is increasing/decreasing by each second?
6. Use the Oscilloscope tool in TinkerCAD. Set its Time Per Division to 1 millisecond. Connect negative to ground and positive to the Anode of corresponding LEDs.
What do the graphs look like? Why?
Questions:
How does PWM differ from simply turning an LED on and off?
What does a PWM Graph look like and why?
What is the duty cycle in the context of PWM, and how does it affect LED brightness?
Why is PWM limited to 255 on an Arduino?
Are there any considerations for power supply when dealing with multiple LEDs?
How does this project extend your knowledge from the basic "Blink" project?
Exercise Three: Serial Monitor and Analog Sensors
Background Knowledge:
How to read a Schematic
How to use a Breadboard
How to read and use a Multimeter
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - 10K Potentiometer
1 - Jumper Wire Set
In this introductory lesson, we will learn how to utilize the Serial Monitor in conjunction with a potentiometer, a variable resistor, to read analog sensor values. This exercise serves as a fundamental step in understanding how to interface analog sensors with Arduino and visualize their output in real-time.
Steps
Set Up the Circuit: Begin by connecting the potentiometer to the Arduino:
Connect one end of the potentiometer to the 5V pin on the Arduino.
Connect the other end to the GND (ground) pin.
Connect the middle pin (wiper) of the potentiometer to analog pin A0 on the Arduino.
2. Write the Arduino Sketch
In TinkerCAD, there is a "Print to Serial Monitor Block" that can have written text typed into it or additional blocks of inputs and outputs for visual output. It is good practice to set an input to a variable that you can name- this will help you keep track of your inputs and outputs in larger projects. These code blocks should be placed in your "Forever" Command block or your Void Loop in C++. This way it refreshes after every delay to update your sensor inputs.
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
}
3. Understand the Code
Serial.begin(9600); initializes communication with the Serial Monitor at a baud rate of 9600 bits per second.
int sensorValue = analogRead(A0); reads the analog value from the potentiometer and stores it in the variable sensorValue.
Serial.println(SensorValue); sends the sensor value to the Serial Monitor for display.
delay(10); introduces a short delay to stabilize readings.
4. Upload and Monitor
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor). You should now see a continuous stream of analog readings from the potentiometer displayed in the Serial Monitor.
5. Experiment and Observe
Rotate the potentiometer knob and observe how the values change. You'll notice that they range from 0 (minimum) to 1023 (maximum), representing the analog input range of the Arduino.
This introductory lesson provides a foundational understanding of how to use the Serial Monitor to read analog sensor values, laying the groundwork for more complex sensor interfacing and data visualization projects in the future.
Questions:
Why is the potentiometer considered an analog sensor?
Why is the Serial Monitor used in this project?
What is the range of values output by the potentiometer, and how does it correspond to its physical position?
Can you convert the potentiometer readings to a percentage or another meaningful unit?
How does this project lay the groundwork for more complex projects involving sensors?
Exercise Four: Serial Monitor and Digital Sensors
Background Knowledge:
How to read a Schematic
How to use a Breadboard
How to read and use a Multimeter
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - N.O. Button
1 - Jumper Wire Set
In this introductory lesson, we will learn how to employ the Serial Monitor in tandem with a digital input, specifically a button. This exercise will introduce you to the process of reading digital signals and visualizing their state changes in real-time using Arduino.
Steps
Set Up the Circuit: Begin by connecting the button to the Arduino:
Connect one leg of the button to digital pin 2 on the Arduino.
Connect the other leg of the button to GND (ground) on the Arduino, using a resistor (pull-down resistor).
Extra Vocabulary! A pull-down resistor holds the logic signal near zero volts (0V) when no other active device is connected. It pulls the input voltage down to the ground to prevent an undefined state at the input.
2. Write the Arduino Sketch
Using what you know from the Serial Monitor with an Analog sensor from above, replicate the same steps but using the digital input blocks and commands.
3. Understand the Code
int buttonPin = 2; assigns pin 2 as the input pin for the button.
int buttonState = 0; initializes the variable to hold the button state.
pinMode(buttonPin, INPUT); sets pin 2 as an input.
buttonState = digitalRead(buttonPin); reads the digital state of the button (HIGH or LOW) and stores it in buttonState.
Serial.println(buttonState); sends the button state to the Serial Monitor for display.
delay(100); introduces a short delay to stabilize readings.
4. Upload and Monitor
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor). You will now see a continuous stream of digital readings (0 or 1) indicating the state of the button.
5. Experiment and Observe
Press and release the button. Observe how the values change from 0 (button not pressed) to 1 (button pressed) in the Serial Monitor.
This introductory lesson provides a foundational understanding of how to use the Serial Monitor to read digital inputs from a button. This knowledge can be applied to a wide range of projects involving switches, sensors, and other digital components.
Questions:
How is the button connected to the Arduino board?
Why is the button considered a digital sensor?
What is debouncing, and why is it important when working with buttons?
What other information can be useful to display in the Serial Monitor when working with a button?
Exercise Five: Basic Logic
Background Knowledge:
How to read a Schematic
How to use a Breadboard
How to read and use a Multimeter
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - N.O. Button
1 - Jumper Wire Set
In this lesson, we'll explore the basics of logic statements in Arduino programming. We'll create a project where an LED turns on if a button is pressed.
Steps
Set Up the Circuit: Begin by connecting the button to the Arduino:
Connect one leg of the button to digital pin 2 on the Arduino.
Connect the other leg of the button to GND (ground) on the Arduino, using a resistor (pull-down resistor).
Insert the LED into the breadboard. The longer leg is the positive (anode) and the shorter leg is the negative (cathode).
Connect the cathode of the LED to a 470 Ohm resistor.
Connect the other end of the resistor to a GND pin on the Arduino.
Connect the anode of the LED to digital pin 13 on the Arduino.
2. Write the Arduino Sketch
Using what you know from the Serial Monitor with an Analog sensor from above, replicate the same steps but using the digital input blocks and commands.
void setup()
{
pinMode(2, INPUT);
pinMode(13, OUTPUT);
}
void loop()
{
if (digitalRead(2) == HIGH) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
delay(10); // Delay a little bit to improve simulation performance
}
3. Understand the Code
pinMode(2, INPUT); sets pin 2 as an input.
pinMode(13, OUTPUT); sets pin 13 as an OUTPUT
if (digitalRead(2) == HIGH) {digitalWrite(13, HIGH)}; If the button is pressed and input 2 is high, then set output pin 13 to high and turn on the LED.
else {digitalWrite(13, LOW); Otherwise, set output pin 13 to low keeping the LED off.
delay(10); introduces a short delay to stabilize readings.
NOTE:
If you only use an IF statement and not an IF-ELSE Statement, then the first time you press the button, the LED will turn on an stay on even if you let go of the button. This is because there is no alternative option for the LED to be explicitly set to. It will be set to the condition and stay there until the program is terminated.
4. Upload and Monitor
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor). You will now see a continuous stream of digital readings (0 or 1) indicating the state of the button.
5. Experiment and Observe
Press and release the button. When you press the button, the LED should turn on. When you release the button, it should turn off.
This introductory lesson provides a foundational understanding of how to use the Serial Monitor to read digital inputs from a button. This knowledge can be applied to a wide range of projects involving switches, sensors, and other digital components.
Questions:
How does the button interact with the LED in terms of logic statements?
Are there any resistors needed in the circuit? Why?
What is the purpose of using logic statements (e.g., if statements) in this project?
How do you structure if statements in Arduino to control the flow of the program?
Can you use logical operators (AND, OR) to create more complex conditions?
Are there scenarios where else/if statements would be more appropriate than a simple if statement?
What considerations are important when dealing with button presses and releases?
Can you use logic statements to create more advanced behaviors, such as toggling the LED with each button press?
How does this project lay the groundwork for more complex projects involving conditional control?
What are common issues that might arise when working with logic statements and button-controlled LEDs and how could you debug them?