Chapter 2 Microcontroller Fundamental and Programming
Chapter 2 Microcontroller Fundamental and Programming
Chapter 2 Microcontroller Fundamental and Programming
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}
void loop() {
digitalWrite(ledPin, HIGH); // Turn the LED on
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn the LED off
delay(1000); // Wait for 1 second
• }
Arduino sketch
• An Arduino sketch is a program written in the
Arduino programming language, which is
based on C/C++. It's used to control and
interact with Arduino microcontrollers.
Some Basic Example
• Reading a Button State This code reads the state of a button connected to
digital pin 2 and turns on the built-in LED when the button is pressed.
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on the LED if the button is pressed
} else {
digitalWrite(ledPin, LOW); // Turn off the LED if the button is not pressed
}
Trying the code on an Arduino Emulator -
Extending Arduino
• To try the code on an Arduino emulator, you can use various
online Arduino simulators that allow you to write, upload,
and run Arduino sketches virtually. One such emulator is
Tinkercad Circuits (previously known as Autodesk
Circuits). Here are the steps to run your code using
Tinkercad Circuits:
1. Visit the Tinkercad Circuits Website: Go to the Tinkercad
Circuits website (https://www.tinkercad.com/circuits/) and
create an account if you don't have one already.
2. Create a New Circuit: Once you're logged in, click on
"Create New Circuit" to start a new project.
3. Add Components: In the circuit editor, you can add
the components you need. To replicate the LED
blinking example, you'll need an Arduino board and
an LED. Search for "Arduino" and "LED" in the
components panel and add them to your workspace.
4. Connect Components: Connect the LED to the
Arduino board, just like you would in a physical
circuit. Connect the longer leg (anode) of the LED to a
digital pin (e.g., pin 13) on the Arduino and the shorter
leg (cathode) to a resistor, and then connect the other
end of the resistor to the Arduino's GND (ground) pin.
5. Write the Sketch: Click on the Arduino board to open
the code editor. You can copy and paste your Arduino
sketch into the editor.
6. Verify and Run: Click the "Start Simulation"
button (a triangle play button) to run your code. You
should see the LED on the virtual Arduino board
start blinking.
• Tinkercad Circuits will simulate the behavior of
the Arduino code, allowing you to test and debug
your projects before you build them physically.
There are also other online Arduino emulators and
simulators available if you prefer a different one.
Arduino Libraries 25 Programming & Interfacing
// Define the interval (in milliseconds) for changing the LED state
const long interval = 1000; // 1000ms = 1 second
// Check if it's time to change the LED state based on the interval
if (currentMillis - previousMillis >= interval) {
// Save the current time as the previous time for the nextinterval
previousMillis = currentMillis;
// Toggle the LED state (if it was LOW, make it HIGH, and vice versa)
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// Sensor settings
#define DHTPIN 2
#define DHTTYPE DHT22
Adafruit_DHT dht(DHTPIN, DHTTYPE);
// LED pin
const int ledPin = 13;
// Timer variables
unsigned long sensorTimer = 0;
unsigned long sendTimer = 0;
const unsigned long sensorInterval = 5000; // Read sensors every 5 seconds
const unsigned long sendInterval = 60000; // Send data every 60 seconds
void setup() {
// Initialize sensors
dht.begin();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
void loop() {
unsigned long currentMillis = millis();
void sendDataToServer() {
// Create a WiFiClientSecure object
WiFiClientSecure client;
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String response = https.getString();
Serial.println(response);
} else {
Serial.println("HTTP Request failed");
}
https.end();
}
void toggleLED() {
if (ledState) {
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
}
ledState = !ledState;
}
Authenticating and Encrypting Arduino Data
byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
0x0C, 0x0D, 0x0E, 0x0F}; // 128-bit encryption key
void setup() {
Serial.begin(9600);
}
void loop() {
char message[] = "Hello, Arduino!"; // Message to encrypt
Serial.print("Encrypted: ");
for (int i = 0; i < 16; i++) {
Serial.print(encrypted[i], HEX);
}
// Decrypt the message
byte decrypted[16];
aes.do_aes_decrypt(encrypted, 16, decrypted, key,
128);
Serial.print("Decrypted: ");
for (int i = 0; i < 16; i++) {
Serial.print((char)decrypted[i]);
}
Serial.println();
Setmode() tells the program to use the Broadcom SOC channel numbers
for pin references. GPIO pins can be referred to using either the Broadcom
numbering (BCM) or the physical pin numbering (BOARD).
# Set the GPIO mode (BCM or BOARD)
GPIO.setmode(GPIO.BCM)
It defines the variable led_pin to store the GPIO pin number (in this case,
17) that's connected to the LED.
# Define the GPIO pin you're using
sets this pin as an output, indicating that you want to control
it to turn an LED on and off.
# Set the pin as an output
GPIO.setup(led_pin, GPIO.OUT)
This part of the code creates a loop that runs indefinitely
using while True
try:
while True:
GPIO.output(led_pin, GPIO.HIGH) # Turn the LED on
turns the LED on by sending a signal to the GPIO pin to
make it HIGH, which typically powers the LED
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup() # Cleanup GPIO pins on
program exit
basic example in Python using the RPi.GPIO library to turn an LED on and off connected to
a GPIO pin:
import RPi.GPIO as GPIO
import time
try:
while True:
GPIO.output(led_pin, GPIO.HIGH) # Turn the LED on
time.sleep(1)
GPIO.output(led_pin, GPIO.LOW) # Turn the LED off
time.sleep(1)
except KeyboardInterrupt: