Foundations of Microcontroller Programming

Lesson 1: High and Low Voltages

Important Topics:

  • Description of GND
  • Description of 5v and 3.3v
  • Description of Anode and Cathode
  • Description of Digital Pins
  • Explanation of Constants (#define)
  • Explanation of pinMode function (OUTPUT)
  • Explanation of digitalWrite function
  • Explanation of delay() function
  • Void setup()
  • Void loop()

Project 1: Turning an LED On and Off With Code

1#define LED 12  //From now on whenever I write LED it means digital pin 12
2
3void setup() {           //The code between these two brackets will only run once
4    pinMode(LED, OUTPUT);  //Making digital pin 12 an output
5}
6
7void loop() {               //The code in between these two brackets will loop or continue forever
8    digitalWrite(LED, HIGH);  //Turning the LED ON
9    delay(250);               //Waiting for 250 milliseconds before moving to the next line of code
10    digitalWrite(LED, LOW);   //Turning the LED OFF
11    delay(250);
12}
C++

Project 2: Turning an LED ON and OFF based on the status of an Electromagnet (With Code)

1#define LED 12  //From now on whenever I right LED it means digital pin 12
2#define MAGNET 13 //From now on whenever I right MAGNET it means digital pin 13
3
4void setup() {           //The code between these two bracket will only run once
5  pinMode(LED, OUTPUT);  //Making digital pin 12 an output
6  pinMode(MAGNET, OUTPUT);
7}
8
9void loop() {								//The code in between these two brackets will loop or continue forever
10  //Whenever the MAGNET is OFF the LED is ON in order to indicate an issue
11  digitalWrite(LED, HIGH);  //Turning the LED ON
12  digitalWrite(MAGNET, LOW); //Turning the MAGNET OFF
13  delay(250);  //Waiting for 250 milliseconds before moving to the next line of code
14
15  //Whenever the MAGNET is ON the LED is OFF to indicate there is no issue
16  digitalWrite(LED, LOW);  //Turning the LED OFF
17  digitalWrite(MAGNET, HIGH); //Turning the MAGNET ON
18  delay(250);
19}
C++

Lesson 2: Buttons

Important Topics:

  • Description of GND
  • Description of Anode and Cathode
  • Description of how buttons control voltage
  • Description of pinMode function (Input)
  • Description of digitalRead function
  • Explanation of Boolean statements

Project 1: Turning an LED ON and OFF using a button (No Code)

Project 2: Turning LED ON and OFF using a switch (With Code)

1#define LED 12     //Whenever LED is used it means digital pin 12
2#define BUTTON 13  //Whenever BUTTON is used it means digital pin 13
3
4int buttonValue;  //buttonValue is declared as an integer
5
6void setup() {
7  pinMode(LED, OUTPUT);   //The LED is an output (it receives information and outputs a response)
8  pinMode(BUTTON, INPUT); //The BUTTON is an input (we decide what to do based on what it tells us)
9}
10
11void loop() {
12  buttonValue = digitalRead(BUTTON);  //This checks whether or not the button was pressed
13 
14  if (buttonValue == HIGH) {          //HIGH means the BUTTON was pressed
15    digitalWrite(LED, HIGH);          //If the BUTTON is pressed than the LED turns on
16  } else {                              //If the BUTTON is not pressed then the LED is OFF
17    digitalWrite(LED, LOW);
18  }
19
20  delay(250);
21}
C++

Lesson 3: Potentiometers

Important Topics:

  • Explanation of Digital and Analog
  • Explanation of voltage in relationship with LEDs
  • Explanation of Potentiometer pinout (Terminal In, Wiper, Terminal GND)
  • Explanation of Digital and Analog Pins
  • Explanation of pinMode function (Input)
  • Explanation of analogRead function

Part 1: Controlling the brightness of an LED using a Potentiometer (No Code)

Part 2: Controlling a Graph in Serial Monitor Using Potentiometer (With Code)

1int pValue; //We are declaring pValue (potentiometerValue) as an integer
2
3void setup() {
4  Serial.begin(9600); //This is needed whenever you want to print something
5  pinMode(A0, INPUT); //We are making A0 an analog input (A stands for analog)
6}
7
8void loop() {
9	//Reads the value on the potentiometer based on how much you turn it
10	pValue=analogRead(A0); //The value can be anywhere between 0 and 1023
11	Serial.println(pValue); //We print the value. The printed value can be placed on the Serial Plotter
12	delay(100);             //Delay is to make things smoother
13}
C++

Lesson 4: Potentiometers (Continued)

Important Topics:

  • Explanation of variables
  • Explanation of incrementing
  • In depth explanation of looping
  • In depth explanation of conditionals
  • Explanation of variables
  • Explanation of analog values

Part 1: Turning Through LEDs in Series Using a Potentiometer (With Code)

1#define LED1 4
2#define LED2 5
3#define LED3 6
4#define POTENT A0
5
6int pValue;
7
8void setup() {
9  Serial.begin(9600);
10  pinMode(POTENT, INPUT);
11  pinMode(LED1, OUTPUT);
12  pinMode(LED2, OUTPUT);
13  pinMode(LED3, OUTPUT);
14}
15
16void loop() {
17  pValue = analogRead(POTENT);
18
19  if(pValue < 341){
20    digitalWrite(LED1, HIGH);
21    digitalWrite(LED2, LOW);
22    digitalWrite(LED3, LOW);
23  }
24 
25  else if(pValue >= 341 && pValue < 675){
26    digitalWrite(LED2, HIGH);
27    digitalWrite(LED1, LOW);
28    digitalWrite(LED3, LOW);
29  }
30 
31  else if(pValue >= 675){
32    digitalWrite(LED3, HIGH);
33    digitalWrite(LED2, LOW);
34    digitalWrite(LED1, LOW);
35  }
36 
37  Serial.println(pValue);
38}
C++

Part 2: Controlling Speed of LEDs in Series Using a Potentiometer (With Code)

1#define LED1 4
2#define LED2 5
3#define LED3 6
4#define POTENT A0
5
6int pValue;
7
8void setup() {
9  Serial.begin(9600); 
10  pinMode(POTENT, INPUT);
11  pinMode(LED1, OUTPUT);
12  pinMode(LED2, OUTPUT);
13  pinMode(LED3, OUTPUT);
14}
15
16void loop() {
17  pValue = analogRead(POTENT);
18
19  digitalWrite(LED1, HIGH);
20  digitalWrite(LED2, LOW);
21  digitalWrite(LED3, LOW);
22  delay(pValue);
23
24  digitalWrite(LED2, HIGH);
25  digitalWrite(LED1, LOW);
26  digitalWrite(LED3, LOW);
27  delay(pValue);
28
29  digitalWrite(LED3, HIGH);
30  digitalWrite(LED1, LOW);
31  digitalWrite(LED2, LOW);
32  delay(pValue);
33 
34  Serial.println(pValue);
35}
C++

Lesson 5: Components

Important Topics:

  • Explanation of importance of transistors in computer engineering
  • Basic explanation of binary
  • Explanation of Base
  • Explanation of Emitter
  • Explanation of Collector
  • How to use a transistor like a button with an LED
  • Explanation of Resistors and Ohms
  • Explanation of capacitors
  • Turning an LED ON and OFF with a Capacitor in between to show the delay in the LED
  • Using stronger resistors between power and an LED to show the decrease in brightness

Project 1: Using a Transistor to turn and LED ON and OFF using a potentiometer (With Code)

1#define POTENT A0
2#define LED 8
3
4int pValue;
5int onStatus;
6
7void setup() {
8  Serial.begin(9600);
9  pinMode(POTENT, INPUT);
10  pinMode(LED, INPUT);
11}
12
13void loop() {
14  pValue = analogRead(POTENT);
15  onStatus = digitalRead(LED);
16 
17  if(onStatus == 1){
18    Serial.println("HIGH");
19  }else{
20    Serial.println("LOW");
21  }
22 
23  Serial.println(pValue);
24}
25
C++

Project 2: Turning two LEDs ON and OFF with a button where one is connected with a Resistor and the other is connected with a wire (With Code)

1#define LEDR 2     //LED connected to a resistor
2#define LED 3     //LED not connected to a resistor
3#define SWITCHR 4 //Switch that turns on the resistor connected LED
4#define SWITCH 5  //Switch that turns on an LED with no resistor connection
5
6int buttonStateR;
7int buttonState;
8
9void setup()
10{
11  Serial.begin(9600);
12  pinMode(LEDR, OUTPUT);
13  pinMode(LED, OUTPUT);
14  pinMode(SWITCHR, INPUT);
15  pinMode(SWITCH, INPUT);
16}
17
18//Within this code we should see the LED not connected to a resistor shine brighter than the one connected to a resistor
19void loop()
20{
21  buttonStateR = digitalRead(SWITCHR);
22  buttonState = digitalRead(SWITCH);
23 
24  if (buttonStateR == 1) {
25    digitalWrite(LEDR, HIGH);
26  }
27 
28  else if(buttonStateR == 0) {
29    digitalWrite(LEDR, LOW);
30  }
31 
32  if(buttonState == 1){
33    digitalWrite(LED, HIGH);
34  }
35 
36  else if(buttonState == 0){
37    digitalWrite(LED, LOW);
38  }
39
40  Serial.println(buttonStateR);
41  delay(250); // Delay a little bit to improve simulation performance
42}
C++

Lesson 6: Combining Everything Learned

Project Ideas:

  • Electronic Piano: Each Beeper controlled with button and the loudness controlled with potentiometer
  • Walk in LED lights your room using sensor
  • Electromagnet you can turn on and off using button and control strength through potentiometer
  • Changing the brightness of an LED based on how many times a button is pushed
  • Changing how loud a beeper is based on how many times you press a button

Lesson 7: Introduction to IOT

Important Topics:

  • What the Cloud is
  • Explanation of libraries
  • What an HTTP Protocol is
  • What an IP address is
  • Types of microcontrollers with Wifi capabilities (ESP32, ESP8266 etc.)
  • What IOT (Internet of Things) means

Project: Creating a button in Blynk to turn an LED ON and OFF

1/*************************************************************
2  This is a simple demo of sending and receiving some data.
3  Be sure to check out other examples!
4 *************************************************************/
5/* Fill-in information from Blynk Device Info here */
6#define BLYNK_TEMPLATE_ID           "TMPL2keuLF-Sw"
7#define BLYNK_TEMPLATE_NAME         "Example"
8#define BLYNK_AUTH_TOKEN            "Aly0dBF-VRM1XXU8L2mQ_2LwUu6A03yU"
9
10#define LED 2
11
12/* Comment this out to disable prints and save space */
13#define BLYNK_PRINT Serial
14
15#include <ESP8266WiFi.h>
16#include <BlynkSimpleEsp8266.h>
17
18// Your WiFi credentials.
19// Set password to "" for open networks.
20char ssid[] = "YourNetworkName";
21char pass[] = "YourPassword";
22
23BlynkTimer timer;
24
25// This function is called every time the Virtual Pin 0 state changes
26BLYNK_WRITE(V0) {
27  // Set incoming value from pin V0 to a variable
28  int value = param.asInt();
29
30  // Update state
31  Blynk.virtualWrite(V1, value);
32}
33
34// This function is called every time the device is connected to the Blynk.Cloud
35BLYNK_CONNECTED() {
36  // Change Web Link Button message to "Congratulations!"
37  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
38  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
39  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
40}
41
42// This function sends Arduino's uptime every second to Virtual Pin 2.
43void myTimerEvent() {
44  // You can send any value at any time.
45  // Please don't send more that 10 values per second.
46  Blynk.virtualWrite(V2, millis() / 1000);
47}
48
49void setup() {
50  // Debug console
51  Serial.begin(115200);
52
53  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
54  // You can also specify server:
55  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
56  //Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(192,168,1,100), 8080);
57
58  // Setup a function to be called every second
59  timer.setInterval(1000L, myTimerEvent);
60
61  pinMode(LED, OUTPUT);
62}
63
64void loop() {
65  Blynk.run();
66  timer.run();
67  // You can inject your own code or combine it with other sketches.
68  // Check other examples on how to communicate with Blynk. Remember
69  // to avoid delay() function!
70
71  if(V1 == 1){
72    digitalWrite(LED, HIGH);
73  }else{
74    digitalWrite(LED, LOW);
75  }
76}
C++