Voltages, currents, and level-shifting 101
GPIO sink/source < 20 mA; total port < 100 mA — drive relays or LED strips with transistors, not pins.
Buttons, LEDs, relays, and more
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();
}
}
Reading voltages, currents, and resistive elements
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
}
From 16×2 LCDs to full-color TFTs
#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"); }
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 dsp(128,64,&Wire,-1);
void setup(){ dsp.begin(SSD1306_SWITCHCAPVCC,0x3C); }
DC, stepper, servo, and ESC driving
#include <Servo.h>
Servo s; void setup(){ s.attach(5); }
void loop(){ s.write(90); delay(1000); s.write(0); }
UART, SPI, I²C, CAN, and 1-Wire
void setup(){ Serial.begin(9600); }
void loop(){ if(Serial.available()) Serial.write(Serial.read()); }
#include <max6675.h>
MAX6675 tc(13,12,11); // SCK,CS,SO
#include <mcp_can.h>
MCP_CAN can(10); void setup(){ can.begin(MCP_ANY,500000,10); }
#include <OneWire.h> #include <DallasTemperature.h>
HTTP, MQTT, BLE, and more
Serial1
.
// AT+NAME="MEGA_BLE"
Serial1.write("Hello from Mega");
#include <Ethernet.h>
EthernetServer srv(80); void loop(){ EthernetClient c=srv.available(); }
SD cards, EEPROM, FRAM, and flash memories
#include <SD.h>
File f; void setup(){ SD.begin(4); f=SD.open("log.csv",FILE_WRITE); }
Serial streams, HID, and REST APIs
import serial, matplotlib.pyplot as plt
ser=serial.Serial('COM5',115200)
johnny-five
(Node.js).
#include <PubSubClient.h>
client.publish("sensors/temp","23.4");
Supplying clean, safe energy to your board
Tools and habits to keep projects reliable
Cheap 8-channel Saleae clones decode SPI/I²C timing issues in minutes.
setupIO()
, readSensors()
, updateActuators()
.SPI.h
vs display libs.library.properties
.