Arduino C++ Study guide
Introduction
Arduino is an open-source electronics platform based on easy-to-use hardware and
software. You write C/C++ code in the Arduino IDE, then upload it to the
microcontroller.
How to Make a Logical Flow Chart for Arduino Coding Projects
What is a Flow Chart?
A flow chart is a diagram that shows the steps of a program or process using
symbols and arrows. It helps you plan and visualise how your Arduino project will
work before you start coding.
Why Use a Flow Chart?
- Helps you think through the logic of your code.
- Makes it easier to debug (find and fix errors).
- Useful for explaining your project to others.
- Shows the order of operations in your Arduino program.
Common Flow Chart Symbols
Steps to Create a Logical Flow Chart
1. Understand the Problem
What is your Arduino project meant to do?
Example: “Turn on a Green LED when a car is detected by the ultrasonic sensor.”
2. List Inputs and Outputs
Inputs = sensors or buttons (e.g., ultrasonic sensor, button)
Outputs = actuators (e.g., LED, buzzer, servo motor)
3. Break the Project into Steps
Think about what happens first, next, and last.
Example:
1. Start the program.
2. Read sensor.
3. Check if the object is close.
4. If YES → activate Green LED.
5. If NO → do nothing.
6. Repeat.
4. Draw the Flow Chart
START
Read
Ultrasonic
Sensor
Turn on Turn on
Green Red
LED Is Object LED
< 10cm?
Repeat
END
5. Test Your Logic
Imagine the flow: does it make sense?
Are all decisions clear?
Are actions in the right order?
Tips for Good Flow Charts
- Use clear, simple language.
- Keep steps short and logical.
- Only one arrow comes out of a process box.
- Two arrows (Yes/No) come out of a decision box.
- Use loops to repeat actions (like in Arduino's loop() function).
Summary Checklist
✔ Know what your project must do
✔ Identify inputs and outputs
✔ Plan the steps clearly
✔ Use the correct symbols
✔ Test your logic before coding
Components
Arduino Uno Main Board
Type: Microcontroller board (Input & Output)
Key details:
- Digital I/O pins (0–13): Read/write digital signals (HIGH/LOW).
- Analog input pins (A0–A5): Read sensors as values 0–1023.
- Power pins (5 V, 3.3 V, GND): Supply or ground your circuits.
- USB port: Power + program upload from computer.
- Reset button: Restarts your sketch.
How it works:
- Write code (a “sketch”) in the Arduino IDE.
- USB connection uploads the compiled code to the ATmega328P.
- The microcontroller executes instructions, reading inputs and driving outputs.
Resistor
Type: Passive component
Key details:
- Limits current flow (neither active input nor output).
- Measured in ohms (Ω); common values: 220 Ω, 1 kΩ, 10 kΩ.
How it works:
- By Ohm’s Law: V = I × R.
- Drops voltage to keep current safe.
- Example: With 5 V supply, LED forward voltage ~2 V, current ≈ (5 V – 2 V) / 220 Ω
≈ 13 mA.
LED (Light Emitting Diode)
Type: Output device (Indicator)
How it works:
- Apply current in the correct direction (anode → cathode).
- Electrons recombine with holes, releasing photons (light).
- Use a series resistor, otherwise, LED or pin can burn out.
Push-Button
Type: Input device (Switch)
Key details:
- Usually 4 pins; opposing pins internally connected.
How it works:
- Unpressed: Circuit open; no current.
- Pressed: Contacts touch; current flows.
- Use pull-down or pull-up so the input pin reads defined HIGH/LOW when
unpressed.
Ultrasonic Sensor (HC-SR04)
Type: Input device (Distance measurement)
Key details:
- Pins: VCC, GND, Trigger, Echo.
How it works:
- Send 10 µs HIGH pulse to Trigger.
- Sensor emits ultrasonic burst (~40 kHz).
- Echo goes HIGH until burst returns.
- Measure duration; distance = (duration/2) × 0.034 cm/µs.
Servo Motor
Type: Output device (Actuator)
Key details:
- Wiring: Power (5 V), GND, Control (PWM) pin.
How it works:
- Send pulse every 20 ms; width 1 ms (0°) to 2 ms (180°).
- Internal circuitry moves shaft using feedback potentiometer.
- Holds position under load.
Buzzer
Type: Output device (Sound indicator)
Key specs:
o Operating voltage: ~3–5 V
o Type: Active (built-in oscillator) or passive (requires PWM)
How it works:
1. Active buzzer: Apply a DC voltage to make it buzz continuously.
2. Passive buzzer: Send a PWM signal (square wave) at the desired
frequency; the piezo element vibrates to produce sound.
3. Use a digital output pin to drive the buzzer; for passive buzzers, you
can vary the PWM frequency in code to change pitch.
RGB LED
RGB LED
Type: Output device (Visual indicator)
Key specs:
o Operating voltage: ~2–3.3 V per color channel (Red, Green, Blue)
o Types: Common anode (shared positive) or common cathode (shared
negative)
How it works:
1. Each of the three internal LEDs (Red, Green, Blue) can be controlled
separately using digital or PWM signals.
2. By mixing brightness levels of each colour channel (via PWM), you can
generate almost any visible colour.
3. Connect each colour pin to a digital/PWM output through a resistor to limit
current.
Soil Moisture Sensor
Type: Input device (Analog/digital sensor)
Key specs:
o Operating voltage: ~3.3–5 V
o Output: Analog (variable voltage) and/or digital (threshold detection via
comparator) Threshold = 539.
o Probes: Two conductive pins measure resistance in soil
How it works:
1. The sensor measures the resistance between the two metal probes; wetter
soil conducts better (lower resistance, higher analogue output).
2. Analog output gives a continuous value representing moisture level.
3. Digital output provides HIGH/LOW based on an adjustable threshold (via
onboard potentiometer).
4. Can be read using an Arduino analogue input pin (for precise moisture levels)
or digital pin (for simple dry/wet detection).
C++ Coding
Data Types
int: A whole number.
float: A decimal number.
string: A word or sentence.
boolean: true or false
Operators
Arithmetic:
+ : Adds numbers
- : Subtract numbers.
*: Multiplies numbers.
/: Divides numbers.
%: Finds the percentage.
Assignment:
=: Assigns (Makes) it equal to something,
Comparison:
==: Compares one thing with another.
Increment/Decrement:
++: increases a value by 1.
--: decreases the value by 1.
Variables
A variable is a named storage location in your program that holds a value of a
specific data type.
You declare a variable by specifying its data type and name, for example:
int counter; // declares an integer variable named "counter"
float temperature; // declares a float variable named "temperature"
You can also initialize a variable when you declare it:
int counter = 0;
Variables allow you to store and update data during program execution, such
as sensor readings or loop counters.
Basic Structure of an Arduino Sketch
Declare your variables
void setup() {
// initialize pin modes, start serial communication, etc.
}
void loop() {
// main code runs here repeatedly
}
Pin Configuration and input and output functions
pinMode (pin, mode);
pin: digital pin number. Where is it connected to?
Mode: INPUT or OUTPUT. Tell it if it is an output or an input.
digitalWrite(pin, value); (digitalWrite: giving an instruction)
digital pin number. Where is it connected to?
Value: HIGH or LOW
digitalRead(pin, value); (digitalRead: Looking for information)
If- else statements:
if (condition) {
// execute when condition is true
} else {
// execute when condition is false
}
Basic TinkerCad circuits and their code:
1. One way traffic light system
Code:
int LED_RED = 13;
int LED_ORANGE = 12;
int LED_GREEN = 11;
void setup()
{
pinMode(LED_RED, OUTPUT);
pinMode(LED_ORANGE, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
}
void loop()
{
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_ORANGE, LOW);
digitalWrite(LED_GREEN, LOW);
delay(1000);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_ORANGE, HIGH);
digitalWrite(LED_GREEN, LOW);
delay(1000);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
delay(1000);
2. Simple buzzer circuit
Code:
int buzzerPin = 8;
void setup () {
pinMode(buzzerPin, OUTPUT);
tone(buzzerPin, 1000, 2000);// tone(What?, Volume(htz), length)
}
void loop () {
tone(buzzerPin, 440); //A4
delay(1000);
tone(buzzerPin, 494); //B4
delay(1000);
tone(buzzerPin, 523); //C4
delay(1000);
tone(buzzerPin, 587); //D4
delay(1000);
tone(buzzerPin, 659); //E4
delay(1000);
tone(buzzerPin, 698); //F4
delay(1000);
tone(buzzerPin, 784); //G4
delay(1000);
noTone(buzzerPin);
delay(1000);
3. Servo Motor with Push Button
Code:
#include <Servo.h>
int Button = 0;
Servo servo_9;
void setup()
{
pinMode(3, INPUT);
servo_9.attach(9, 500, 2500);
}
void loop()
{
Button = digitalRead(3);
if (Button == 1) {
servo_9.write(180);
} else {
servo_9.write(0);
}
delay(10);
}
4. Ultrasonic Sensor and Test
Code:
int echoPin = 10;
int trigPin = 9;
float time =0.0;
float distance = 0.0;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
digitalWrite(trigPin,LOW);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trigPin,LOW);
delay(2);
digitalWrite(trigPin,HIGH);
delay(20);
digitalWrite(trigPin,LOW);
time = pulseIn(echoPin, HIGH);
distance = (time *0.034)/2;
Serial.print("Distance: ");
Serial.print(distance);
}
5. RGB LED and Button
Code:
int Button = 2;
int LED_Red = 6;
int LED_Blue = 5;
int LED_Green = 3;
void setup()
{
pinMode(2, INPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(3, OUTPUT);
Serial.begin(9600);
}
void loop()
{
Button = digitalRead(2);
Serial.print(Button);
if (Button == 1) {
analogWrite(LED_Red, 255);
analogWrite(LED_Blue, 0);
analogWrite(LED_Green, 0);
delay(1000); // Delay a little bit to improve simulation performance
} else {
analogWrite(LED_Red, 0);
analogWrite(LED_Blue, 255);
analogWrite(LED_Green, 0);
delay(1000); // Delay a little bit to improve simulation performance
}
delay(10);
}
6. Moisture Sensor
Code:
int moistureValue;
int moisture_percentage;
void setup ()
{
Serial.begin(9600);
}
void loop()
{
moistureValue = analogRead(A0);
moisture_percentage = ((moistureValue/539.00)*100);
Serial.print("*\Moisture Value : ");
Serial.print(moisture_percentage);
Serial.print(" ");
Serial.print (moistureValue);
Serial.print("%");
delay(2000);
}