Introduction to Microcontrollers

Overview

Microcontrollers are basically a miniature personal computer on a single integrated circuit (IC), which are sometimes referred to as a "chip" or "microchip". They generally include a processor, memory, and input/output peripherals on a single chip. Microcontrollers are used for all sorts of applications. They can be found in vehicles, robots, office machines, medical devices, vending machines, and home appliances, among other devices.

Microcontrollers are generally found as 8-bit or 32-bit chips. This refers to the amount of bits of data a central processing unit (CPU) can process or move in and out of memory at a time. In recent years, microcontroller development boards have become very popular among artists, designers, engineers, students, and hobbyists. These boards include a microcontroller as well as various analog and digital input and output pins. Some well known boards include Arduino, Raspberry Pi, and Adafruit Circuit Playground Express. Common programming languages for popular development boards include C/C++, Python, and JavaScript.

Electronics Basics

Some basic electronics knowledge will be necessary to complete this project, largely, vocabulary and theory. An attempt has been made to only include vocabulary and theory that will be needed to be successful in this activity.

Types of Circuits

A circuit can either be a series circuit, parallel circuit, or a combination of the two (series-parallel circuit). Depicted below are schematic diagrams of a series and a parallel circuit. Schematic diagrams represent the circuit and offer a cleaner, more efficient way of viewing electrical circuits. A series circuit has only one path for the electrical current to pass through. The current will pass in turn through each component in the circuit. A parallel circuit has multiple pathways that current flows through at the same time. Current divides between all of the paths before eventually combining again. For the sake of a quick example, you likely have a room in your house where multiple lights turn on with one switch. If you remove one of the light bulbs out, the rest of the lights stay on. That indicates that this circuit is in parallel. If you have ever worked with cheap or older string lights that have bulbs and not LEDs, when one burns out, the entire string goes out, indicating the lights are in series.

Ohm's Law

Ohm's law is a law that states that the voltage across a resistor is directly proportional to the current flowing through the resistance. Ohm's law is named for German physicist Georg Ohm (1789-1854). The law uses a simple formula to show the relationship between current, voltage, and resistance.

An electric current (I) is the rate of flow of electric charge past a point or region. The measure of current is the ampere, often shortened to amps. Voltage (ΔV or E ), measured in volts (v), is the pressure from an electrical circuit's power source that pushes charged electrons (current) through a conducting loop. Electrical resistance (R) of an object is a measure of its opposition to the flow of electric current. Resistance is measured in ohms (Ω). The symbol used to represent ohms is the Greek letter Omega. The image below is a good depiction of the law.

Ohm's Law Formula

Basic Electronics Components

Resistor

A resistor is used to eliminate excess voltage in a circuit, which can damage electronic components.

Light Emitting Diode

A light emitting diode or LED is a polarized light source, meaning that if the positive and negative terminals are placed in the wrong configuration, the light will not illuminate.

Circuits In Tinkercad

Simulation Using Tinkercad

One great thing about Tinkercad is it allows you to build, test, and debug your circuits with a simulator before actually creating the physical model. The benefit being, you can work out all of the flaws without damaging your components. The video below shows a password-protected lock built and tested in Tinkercad, then uploaded into the Arduino IDE, and finally uploaded into the Arduino board.

Opening Tinkercad and the Circuit

For this project, you will log into Tinkercad using Google. Select "Join Class" on the left side of the screen once you get in. You will receive the class code from your teacher. Check to see if it is posted in Classroom. If not, ask your teacher for the code.

Once you are logged in, open Classroom and find the appropriate assignment - named something to do with Tinkercad and microcontrollers. Click the link to the circuit or access the circuit here. You will have the option to "Copy and Tinker" the circuit. Select that option.

The circuit is embedded below and can be simulated below, however, the circuit and code cannot be altered. You will need to follow the steps listed on this page to complete this activity. Initially, start the simulation and simply observe. The LED will power on for one second and off for one second in a loop (repeatedly while the simulation is running).

Exploring Resistance in a Circuit

Follow the steps below to make specific adjustments to the circuit and record your observations. You will find information on Classroom regarding the method that should be used to document your findings, such as a worksheet or form.

  1. While the circuit simulation is still running, click on the resistor.

  2. Using the drop-down menu, change the units from ohm's (Ω) to picoohms (pΩ). This changes the resistance from 220 Ω to 0.00000000022 Ω. Make note of what occurs in the circuit.

  3. Now change the units to kiloohms (kΩ). This changes the resistance from 220 Ω to 220,000 Ω. Make note of what occurs in the circuit.

  4. Next, change the units to megaohms (MΩ). This changes the resistance from 220 Ω to 220,000,000 Ω Make note of what occurs in the circuit.

  5. Change the units back to ohms (Ω).

Arduino Code

Arduino language is a set of C/C++ functions that can be called to the code. Tinkercad allows you to program with block code (like Scratch or Blockly) but for this project you will be using text coding. You will be examining the code and making specific adjustments to the code to see how they affect the operation of the simulated Arduino circuit. There are some computer science concepts and terms that you will need to be familiar with before moving on to the circuit.

With Arduino, programs are called sketches. An Arduino sketch contains the code that will be uploaded into the board.

You may notice there are three distinct parts to this sketch, highlighted above. The text in the red box starts with a slash asterisk /* and ends with a asterisk slash */. In C/C++, this is used for multi line comments and can be used anywhere in the code.

The section in the blue box uses the void function. We will not get into this specific function at this level but the concept of a function is important. A function (also known as a method) is a block of reusable code that performs a specific action. The void setup() portion of this code must be in all sketches and is only run once after each power up or reset of the board. It is used to setup your inputs and outputs needed in the program. In this case, it is used to tell pin 13 that it will be an output in this program, in other words, it will send signals out from that pin on the board.

The section in the green box also uses the void function, but it is a loop. A loop function will run the program line by line until the end and then repeat, hence "loop". Each statement in the function body also contains comments. Unlike the comment at the beginning of the sketch, these are single line comments, which start with double slashes //. Leaving comments is very helpful in programming, helping others make sense of the code or even yourself when revisiting code that you have not looked at recently. Also, notice that each line of the function body is indented four spaces from the initial function (void loop). This is necessary. The program will not work, otherwise. Also, there are curly braces { } at the beginning and end of the function body and a semicolon ; after each statement. These are all absolutely necessary and are referred to as syntax. the syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be correctly structured statements or expressions in that language. Unlike a human, a computer cannot make inferences. If you wrote a note and forgot to capitalize the first word in a sentence or left out a comma or period, someone will still be able to read your note. A computer is not capable of this. It is only capable of doing exactly what it is told and must be told using the proper syntax for the computer language being used. This will be explored in this activity.

Exploring Arduino Code

Follow the steps below precisely to make specific adjustments to the circuit and record your observations, as you did when working with resistance. You will find information on Classroom regarding the method that should be used to document your findings.

  1. Open the code for the Basic Blinking LED circuit (the Code button next to the Start Simulation button).

  2. Read through the comments in the sketch. Be sure to ask questions if anything does not make sense.

  3. Make sure the simulation is stopped.

  4. On line 6, capitalize the V in void setup ().

  5. Run the simulation. Take note of the error message. This is a syntax error. Notice that it offers that maybe you meant "void" instead of "Void". When you have errors in your code, you will receive error messages like this one.

  6. Change the V in "void" back to a lowercase V.

  7. On line 16, change the delay function (delay(1000)) from 1000 to 100.

  8. Run the simulation. Make note of what occurs in the circuit.

  9. Change the delay function on line 16 back to 1000.

  10. Run the simulation and verify that the light blinks on for one second and off for one second, again.


Challenge 1: Railroad Crossing Lights

For this challenge, you will be adjusting the Basic Blinking LED circuit to make railroad crossing lights. Do not remove any code from the existing circuit, only add to it. You will also be expected to add comments to describe the code that you add. The requirements below need to be met.

  1. Start with the Basic Blinking LED circuit.

  2. Add a green LED and attach it to pin 9. Remember, LEDs are polarized components, so the anode (+) must be attached to pin 9 and the cathode (-) must be attached back to a ground pin (GND). Do not forget to setup pin 9 in the void setup() section.

  3. Add code to make the green LED light when the red LED is off and vice versa, so they will alternate flashing on and off.

  4. Add a comment to each line of code that you add, explaining what that line does.

  5. Update existing comments so they make more sense with the addition of an extra LED.

  6. Add a link to your completed circuit to the appropriate assignment on Classroom.

** Hint: you can copy and past lines of code and simply adjust them to accomplish this task.**

Your design should work like this:

Challenge 2: Traffic Light

For this challenge, you will be finishing a partially completed project. A link to the circuit can be found in Classroom. The assignment will be named Challenge 2 - Traffic Light or something very similar. First, make sure you are logged into Tinkercad. Next, when you click the project link in Classroom, you will have the option to "Copy and Tinker" the circuit. Select that option. Do not remove any code from the existing circuit, only add to it. You will also be expected to add comments to describe the code that you add. The requirements below need to be met.

  1. The red LED lights for 4 seconds

  2. The red LED turns off

  3. The green LED lights for 4 seconds

  4. The green LED turns off

  5. The yellow LED lights for 2 seconds

  6. The yellow LED turns off

  7. The programs loops back to red and repeats the cycle

  • Add comments to explain the code that you've added.

  • Add a link to your completed circuit to the appropriate assignment on Classroom.

** Hint: you can copy and past lines of code and simply adjust them to accomplish this task.**

Your design should work like this:

Challenge 3: Two Way Intersection Traffic Lights

For this challenge, you will make a copy of your completed Traffic Light circuit from challenge 2. You can duplicate circuits by clicking on the Options icon (a gear) in the corner of your file when you mouseover it. Change the name of your copy to Challenge 3 - Two Way Traffic Lights. Do not remove any code from the existing circuit, only add to it, or adjust it in a few cases . You will also be expected to add comments to describe the code that you add. The requirements below need to be met.

  • Make a copy of the Challenge 2 - Traffic Light circuit in the Tinkercad Dashboard.

  • Change the name of the copied circuit to "Challenge 3 - Two Way Traffic Lights".

  • The project should run as follows:

Light 1

  1. The red LED lights for 5 seconds

  2. The red LED turns off

  3. The green LED lights for 3 seconds

  4. The green LED turns off

  5. The yellow LED lights for 2 seconds

  6. The yellow LED turns off

  7. The programs loops back to red and repeats the cycle

Light 2:

  1. The green LED lights for 3 seconds

  2. The green LED turns off

  3. The yellow LED lights for 2 seconds

  4. The yellow LED turns off

  5. The red LED lights for 5 seconds

  6. The red LED turns off

  7. The programs loops back to green and repeats the cycle

  • Add comments to explain the code that you've added.

  • Add a link to your completed circuit to the appropriate assignment on Classroom.

** Hint: you can copy and past lines of code and simply adjust them to accomplish this task.**

Your design should work like this: