Electronics Advanced: Microcontrollers, PCBs and Professional Practice
Microcontrollers and Arduino
In this lesson you will
- Describe what a microcontroller is and what its pins can do
- Read the structure of a simple Arduino program
- Use digital I/O, analogue input and PWM within pin limits
Introduction
A microcontroller is a tiny computer on one chip, with memory, a processor and input and output pins. It runs a program that reads sensors and controls LEDs, motors and displays. Boards such as the Arduino make this easy for learners. The code here is shown for reading, and you run it on a real board with the free Arduino software.
The simple explanation
A microcontroller reads inputs, decides using your code, and switches outputs, over and over, very fast.
What the pins can do
- Digital I/O: a pin is an input or an output, reading or driving high and low.
- Analogue inputs: an analogue-to-digital converter (ADC) measures a voltage and turns it into a number.
- PWM outputs: pulse-width modulation simulates varying power by switching on and off quickly.
- Communication pins: serial, I2C and SPI let chips and modules talk to each other.
Pin limits matter. A pin can supply only a small current, typically around 20 mA on common 5 V boards (check your board's datasheet for exact ratings), and the whole chip has a total limit. Never connect a motor or a large LED strip directly to a pin. Use a transistor or MOSFET, as you learned, and use a series resistor for every LED.
Structure of an Arduino program
Every program has two functions: setup() runs once at the start, and loop() repeats forever.
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // use the on-board LED as an output
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // LED on
delay(1000); // wait one second
digitalWrite(LED_BUILTIN, LOW); // LED off
delay(1000);
}This is the classic "blink" program, and it flashes the board's LED once per two seconds.
Quick quiz
In an Arduino program, how often does the loop function run?
Reading a button
With a pull-up resistor (the chip has built-in ones you can enable), a pressed button connects the pin to ground and reads LOW:
const int buttonPin = 2;
const int ledPin = 13;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == LOW) { // pressed
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}Remember switch bounce. For counting presses, add a short debounce delay or a proper library.
Analogue input and the ADC
On a typical board with a 10-bit ADC and a 5 V reference, analogRead() returns a number from 0 to 1023. Convert it to a voltage:
Voltage = reading × 5.0 ÷ 1023A reading of 512 is about 2.5 V. Pair this with the voltage divider idea: a light sensor or thermistor in a divider turns a changing resistance into a voltage you can read.
void setup() {
Serial.begin(9600);
}
void loop() {
int reading = analogRead(A0);
float volts = reading * 5.0 / 1023.0;
Serial.println(volts);
delay(500);
}Serial sends text to your computer, so you can watch values in the serial monitor. It is the best way to debug.
PWM: dimming and speed control
PWM switches a pin on and off very quickly, and the duty cycle, the fraction of time it is on, sets the average power. analogWrite(pin, value) takes a value from 0 (always off) to 255 (always on). 128 is about 50 percent. Use it to dim LEDs, set motor speed (through a transistor) and drive servos.
analogWrite(9, 128); // about half brightness on a PWM pinGood practice
- Avoid long
delay()calls in bigger programs, since nothing else can run. Use timing withmillis()instead. - Name constants rather than scattering pin numbers.
- Comment your code, and keep it simple.
- Power carefully: use the correct voltage, a common ground between boards and modules, and a supply with enough current.
- Protect inputs from voltages outside the allowed range.
- Keep learning with the official reference, examples and libraries.
Try it yourself
On a board you have, or a free online simulator, make an LED fade up and down using analogWrite in a loop, and read a potentiometer with analogRead to control its brightness. Print the readings to the serial monitor.
Summary
- A microcontroller reads inputs and drives outputs under program control, with strict limits on pin current.
- Arduino programs have
setup()once andloop()repeatedly, withdigitalRead,digitalWrite,analogReadandanalogWrite. - Convert ADC readings with reading × 5 ÷ 1023, switch bigger loads through transistors, and debounce inputs.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading