Electrical Foundations

Voltages, currents, and level-shifting 101

1 ‒ Logic Standards

  1. AVR boards (Uno, Mega) use 5 V CMOS; SAMD & ESP cores run at 3 .3 V.
  2. Respect VIH = 0.6 × VCC for reliable HIGH detection.
  3. Shift bi-directional lines (I²C, SPI MISO) with BSS138 MOSFET level shifters.

2 ‒ Current Limits

GPIO sink/source < 20 mA; total port < 100 mA — drive relays or LED strips with transistors, not pins.

Digital I/O

Buttons, LEDs, relays, and more

1 ‒ Debounced Push-Button


const uint8_t BTN=2, LED=13;
void setup() {
  pinMode(BTN, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
}
void loop() {
  static uint32_t t=0;                   // debounce timer
  if(!digitalRead(BTN) && millis()-t>30){
    digitalWrite(LED, !digitalRead(LED));
    t = millis();
  }
}

2 ‒ TIP120 Relay Driver

  1. Connect Base → 1 kΩ → Arduino pin.
  2. Collector → Relay coil.
  3. Emitter → GND, add fly-back diode across coil.

Analog Sensors

Reading voltages, currents, and resistive elements

1 ‒ Thermistor (Voltage Divider)


const float R_FIXED=10000.0F; // 10 kΩ
const uint8_t THM=A0;
void loop() {
  int adc = analogRead(THM);
  float v   = adc * (5.0/1023);
  float r_t = (5.0 - v)*R_FIXED / v;
  // Steinhart-Hart
}

2 ‒ 4-20 mA Current Loop

  1. Insert 250 Ω shunt → 1–5 V range.
  2. Use analogRead() then map 1 V→0 % to 5 V→100 %.

Displays

From 16×2 LCDs to full-color TFTs

1 ‒ 16×2 Character LCD (Parallel)


#include <LiquidCrystal.h>
LiquidCrystal lcd(7,8,9,10,11,12); // RS,E,D4–D7
void setup(){ lcd.begin(16,2); }
void loop(){ lcd.setCursor(0,0); lcd.print("Hello"); }

2 ‒ SSD1306 OLED (I²C)


#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 dsp(128,64,&Wire,-1);
void setup(){ dsp.begin(SSD1306_SWITCHCAPVCC,0x3C); }

Motors & Actuators

DC, stepper, servo, and ESC driving

1 ‒ Servo Pulse


#include <Servo.h>
Servo s; void setup(){ s.attach(5); }
void loop(){ s.write(90); delay(1000); s.write(0); }

2 ‒ L298N H-Bridge (DC/Stepper)

  1. IN1/IN2 ← Arduino; ENA ← PWM for speed.
  2. 12 V rail powers motor, 5 V logic to VCC.

Communication Buses

UART, SPI, I²C, CAN, and 1-Wire

1 ‒ UART Example (9600 Bd)


void setup(){ Serial.begin(9600); }
void loop(){ if(Serial.available()) Serial.write(Serial.read()); }

2 ‒ SPI Sensor (MAX6675 Thermocouple)


#include <max6675.h>
MAX6675 tc(13,12,11);                      // SCK,CS,SO

3 ‒ I²C Multi-Sensor Bus

  1. Pull-ups: 4 .7 kΩ to VCC.
  2. Scan: run Wire.begin() + I²CScanner sketch.

4 ‒ CAN Bus (MCP2515)


#include <mcp_can.h>
MCP_CAN can(10); void setup(){ can.begin(MCP_ANY,500000,10); }

5 ‒ 1-Wire Temperature Chain


#include <OneWire.h> #include <DallasTemperature.h>

Wireless & Networking

HTTP, MQTT, BLE, and more

1 ‒ ESP-01 Wi-Fi Bridge

  1. Flash AT-firmware, wire RX/TX to Serial1.
  2. AT+CWJAP="SSID","PASS"
  3. Stream sensor JSON via HTTP POST.

2 ‒ HC-05 Classic Bluetooth


// AT+NAME="MEGA_BLE"
Serial1.write("Hello from Mega");

3 ‒ W5500 Ethernet Shield


#include <Ethernet.h>
EthernetServer srv(80); void loop(){ EthernetClient c=srv.available(); }

Storage & Data Logging

SD cards, EEPROM, FRAM, and flash memories

1 ‒ SD Card (FAT-FS)


#include <SD.h>
File f; void setup(){ SD.begin(4); f=SD.open("log.csv",FILE_WRITE); }

2 ‒ 24LC256 EEPROM (I²C)

PC & Cloud Integration

Serial streams, HID, and REST APIs

1 ‒ pySerial Plotter


import serial, matplotlib.pyplot as plt
ser=serial.Serial('COM5',115200)

2 ‒ Firmata for Rapid Prototyping

  1. Flash StandardFirmata sketch.
  2. Control pins via johnny-five (Node.js).

3 ‒ MQTT Telemetry


#include <PubSubClient.h>
client.publish("sensors/temp","23.4");

Power & Protection

Supplying clean, safe energy to your board

1 ‒ Regulators & Batteries

2 ‒ Reverse & Surge Protection

  1. Schottky diode on VIN for reverse-polarity.
  2. TVS diode for inductive kickback.

Debugging & Best Practices

Tools and habits to keep projects reliable

1 ‒ Logic Analyzer

Cheap 8-channel Saleae clones decode SPI/I²C timing issues in minutes.

2 ‒ Structured Code

  1. Separate setupIO(), readSensors(), updateActuators().
  2. Keep ISRs <50 µs; defer math to main loop.

3 ‒ Library Hygiene