Get Started
Electronics Basics
On this page
  1. Digital Input and Output
  2. Analog Signals and ADCs
  3. Calculating an LED Resistor
  4. Pull-up and Pull-down Resistors
  5. Converting Between 3.3V and 5V
  6. Serial Communication Standards
  7. Motors and Relays
  8. Multithreading and Execution Interval

Electronics Basics

What to know before wiring a device.

Digital Input and Output

In a digital circuit, current flows when the switch closes and stops when it opens. The closed state is HIGH and the open state is LOW. A digital input is the controller sensing that opening and closing on an external switch; a digital output is the controller driving it.

Digital input circuit: the controller detects whether current flows through an external switch
Digital input: the controller detects whether current flows through an external switch.
Digital output circuit: the controller operates an internal switch to drive current through the load
Digital output: the controller operates a switch to start or stop current.
Wiring diagram of a push button (digital input) and an LED with a resistor (digital output) on a Raspberry Pi
Raspberry Pi example: pressing the button lets current flow so the input reads HIGH, and closing the output pin's internal switch lights the LED.

Analog Signals and ADCs

Values that vary continuously, such as light level or sound volume, are analog signals. The controller only handles digital signals, so reading an analog signal needs an ADC (Analog-Digital Converter) and producing one needs a DAC. The Raspberry Pi has no analog input pins, so register an ADC device such as the ADS1115 as an I/O device.

A digital signal has only two states: current flowing (ON, 1, true) or not flowing (OFF, 0, false). A light switch and a computer's data processing are digital; light level, sound volume, and wind strength are analog. Sound entering a microphone (an analog voltage), for example, is converted to digital data by an ADC, processed, then converted back to an analog voltage by a DAC and played by a speaker. For the units of digital data, see Bits and Bytes.

Flow of a microphone's analog signal converted to digital data by an ADC, processed, and output through a DAC to a speaker
Signal flow for sound: analog → ADC → digital processing → DAC → analog

Calculating an LED Resistor

When the supply voltage is higher than the LED's forward voltage, a resistor must limit the current. Without one, current far above the rating flows and the LED is damaged. Ohm's law gives the value.

R = (supply voltage - LED forward voltage) / current

For a red LED with a 2V forward voltage on a 3.3V GPIO, allowing for the Raspberry Pi's per-pin output limit of about 8mA and targeting 5mA, the result is about 260Ω. The commonly available 220Ω gives about 5.9mA, comfortably within range. The resistor may go on either leg of the LED.

An LED's forward voltage and forward current are listed in its datasheet. For a red LED rated at 2V and 20mA, Ohm's law (V = I × R) gives (3.3V − 2V) / 0.02A = 65Ω, but that exceeds the Raspberry Pi's GPIO output limits (about 8mA per pin, 50mA in total), so the value is recalculated at 5mA as above. With less than the rated 20mA the LED is somewhat dimmer, but bright enough for normal use.

Wiring diagram of a resistor and red LED connected to a Raspberry Pi GPIO pin on a breadboard
A resistor and LED connected to a Raspberry Pi
LED forward voltage by color and recommended resistors

Typical values to refer to when no datasheet is available. Actual values vary by product.

ColorForward voltageTypical current
Red1.8–2.3V20mA
Orange2.0–2.3V30mA
Yellow2.0–2.8V20mA
Green (yellow-green)1.8–2.3V20mA
Green (true green)3.0–3.6V20mA
Blue3.4–3.8V20mA
Pink3.4–3.8V20mA
White3.4–4.0V20mA

Approximate resistor values for a Raspberry Pi (3.3V), calculated as above:

ColorResistor
Red220Ω
Yellow180Ω
Green220Ω
White100Ω

Pull-up and Pull-down Resistors

With a push button released, an input pin connected to neither the supply nor ground is neither HIGH nor LOW but floating. In that state, noise or static can flip the value arbitrarily and it cannot be controlled. A pull-up resistor pins the released state to HIGH and a pull-down resistor pins it to LOW, removing the problem.

An input pin reads HIGH when connected to the supply (VCC) and LOW when connected to GND. If a push button is wired only to GND, as on the left below, the input pin is tied to GND and reads 0V (LOW) while pressed, but floats at an unknown voltage while released.

Push button wired only to GND: the input voltage is unknown (? V) when released and 0V when pressed
A circuit without pull-up or pull-down: the input voltage while released is unknown.

With a pull-up resistor between the input pin and VCC, current flows into the input pin while the button is released, giving HIGH, and flows to GND while it is pressed, giving LOW.

Pull-up resistor between the input pin and VCC: HIGH when released, LOW when pressed
Pull-up resistor circuit

The input pin's impedance (100kΩ to 1MΩ) is far larger than the pull-up resistor, so the two act as resistors in series and almost no voltage drops across the pull-up. The input pin therefore sees a voltage close to VCC.

Equivalent circuit of the pull-up resistor in series with the input pin impedance, with almost no voltage drop across the pull-up
The pull-up resistor and the input impedance

A pull-down resistor goes between the input pin and GND. It pins the released state to LOW and prevents excessive current (a short circuit) to GND while the button is pressed.

Pull-down resistor between the input pin and GND: LOW when released, HIGH when pressed
Pull-down resistor circuit
Note Raspberry Pi and similar boards have internal pull-up and pull-down resistors that can be enabled in software, and their values suit input devices such as push buttons, so no external resistor is needed. Set them in the INIT command of I/O Device → Digital Read/Write. This setting works only on Linux kernel 5.10 or later (Raspberry Pi OS, Ubuntu 22 or later, Debian 11 or later). External resistors can still be added when needed.

Converting Between 3.3V and 5V

Raspberry Pi GPIO runs at 3.3V. Connecting a 5V-only sensor, or a 5V board such as an Arduino Uno, requires level conversion; connecting 5V without it damages the board.

  • Voltage divider: Two resistors lower the voltage. To bring 5V down to 3.3V, for example, R1 of 1kΩ with R2 of 2kΩ produces about 3.3V. Use this for incoming 5V signals such as the echo pin of an HC-SR04 ultrasonic sensor.
  • Logic level converter: A module supporting bidirectional conversion across several channels. Use it for two-way signals such as communication lines, wiring the higher voltage to HV and the lower to LV.

Raspberry Pi, Jetson Nano, and the Arduino Due, Zero, and Nano 33 IoT are 3.3V devices; the Arduino Uno, Nano, Mega, and Leonardo are 5V devices. Level conversion is needed in these cases:

  • Connecting a sensor or module with a 3.3V output to a 5V device
  • Connecting a sensor or module with a 5V output to a 3.3V device
  • Communication between a 3.3V device and a 5V device

The output voltage of a divider is given below, with R1 on the input side and R2 on the GND side.

Vout = R2 / (R1 + R2) × Vin
Wiring diagram of an HC-SR04 ultrasonic sensor's echo output divided down by two resistors to a Raspberry Pi GPIO pin
The HC-SR04 echo pin (5V output) brought down to 3.3V with a voltage divider

Logic level converters come in unidirectional types, which convert one way only, and bidirectional types, which convert both ways. Use a bidirectional type for links with transmit and receive lines such as UART.

Wiring diagram connecting an Arduino Uno and a Raspberry Pi UART through a bidirectional logic level converter, 5V side on HV and 3.3V side on LV
UART between an Arduino Uno (5V) and a Raspberry Pi (3.3V) through a bidirectional logic level converter

Serial Communication Standards

A UART (Universal Asynchronous Receiver/Transmitter) is hardware that sends and receives data serially; "serial communication" usually means communication over a UART. It communicates one-to-one over two pins, TX (transmit) and RX (receive), converting the CPU's parallel data into serial data for sending and received serial data back into parallel data.

UART communication is asynchronous, so both devices must use the same baud rate, number of data bits, parity type, and number of stop bits. These are the communication settings of Modbus RTU and User Defined Communication (serial).

StandardCharacteristics
TTL UARTWires the board's UART pins directly. Suited to short distances and one-to-one links.
RS-232The standard PC serial interface. One-to-one, and its voltage levels differ from TTL, so a converter is needed.
RS-422Differential signaling for longer distances, used for one-to-many in one direction.
RS-485Differential signaling that tolerates long runs and puts several devices on one pair. Widely used for industrial Modbus RTU.
TTL Levels

TTL (Transistor-Transistor Logic) levels are one way of defining the voltage ranges read as HIGH (ON, 1) and LOW (OFF, 0). The 5V TTL levels are:

StateInputOutput
HIGH (ON, 1)2–5V2.7–5V
LOW (OFF, 0)0–0.8V0–0.4V

UARTs use TTL levels. With low voltage levels and a small noise margin (the gap between the input and output thresholds), they are sensitive to noise and limited in distance, so they are mainly used inside a device or over short distances.

RS-232, RS-422, RS-485

These standards make up for the UART's weaknesses, tolerating noise and reaching further. They cover hardware and voltage levels; the communication protocol is still UART.

ItemRS-232RS-422RS-485
SignalingSingle-endedDifferentialDifferential
Network1:11:N (multi-drop)1:N (multi-drop)
Wiring3 wires: TX, RX, GND, with TX and RX crossed between the two ends4 wires: TX+, TX-, RX+, RX-2-wire or 4-wire
Maximum distanceAbout 15mAbout 1.2kmAbout 1.2km
Maximum speed20kb/s10Mb/s10Mb/s
Maximum output voltage±25V-0.25V to +6V-7V to +12V
Maximum input voltage±15V-7V to +7V-7V to +12V
Devices21 master, up to 10 slavesUp to 32; any device can be master
  • RS-232: Uses ±12V levels to reach further.
  • RS-422: Differential signaling, where the same noise on the + and - lines cancels out, makes it noise tolerant. With the master fixed to a single device, it is not a true multi-drop setup.
  • RS-485: Similar to RS-422, but any device on the network can be master or slave. On long runs, termination resistors are sometimes fitted at both ends of the line to reduce noise.
Note A master issues commands to other devices; a slave receives the master's commands and processes or answers them.

Motors and Relays

DC motors are controlled by voltage for speed, servo motors by a PWM signal for angle, and stepper motors by pulse count for rotation. All three are supported as I/O devices (Part 7).

  • DC motor: High starting torque, with output torque proportional to input current. Reversing the polarity reverses the rotation. A motor driver allows forward and reverse rotation and speed control with a PWM signal.
  • Servo motor: Commonly called an RC servo; it turns to and holds an angle between 0 and 180 degrees according to the pulse width of a PWM signal. It is not suited to continuous rotation or speed control, and differs from industrial servo motors.
  • Stepper motor: Turns one step per input pulse. Its high resolution suits precise position and speed control, as in 3D printers and production equipment. It is driven through a stepper driver such as the A4988, which must receive exactly as many pulses as steps, at accurate timing.
Servo control signal timing: at a 20ms (50Hz) period, a 1ms pulse is 0 degrees, 1.5ms is 90 degrees, and 2ms is 180 degrees
Servo PWM signal: at a 20ms (50Hz) period, a 1ms pulse is 0 degrees, 1.5ms is 90 degrees, and 2ms is 180 degrees.

A relay is an automatic switch operated by an electrical signal, also called an electromagnetic relay. Instead of pressing a switch by hand, a weak electrical signal opens and closes its contacts. Controller outputs are limited (DC 5V at up to 40mA on an Arduino, DC 3.3V at about 8mA per pin on a Raspberry Pi), so large loads are switched through a relay. Relays come in two kinds:

  • Electromechanical relay: A small current through the coil closes the contacts magnetically; when the current stops, a spring opens them.
  • Solid state relay (SSR): Switches with semiconductors and no moving parts. It switches fast and lasts long.
Caution Motors draw too much current to be driven from the board's power pins. Always use a separate external supply and a motor driver. For the same reason, a controller output (3.3V at about 8mA) cannot switch mains AC appliances or other high-current loads directly; use a relay module so the controller's signal opens and closes a separate power circuit.

Multithreading and Execution Interval

A thread is a unit of work that runs independently inside a program, and multithreading is one program handling several tasks at once through several threads. Each Grablo logic also runs in its own thread.

One CPU core runs only one task at a time, so when there are more threads than cores the operating system allocates the CPU to them in turn. This is called scheduling, and switching the running task is called context switching. As a result, each thread's execution time varies with CPU performance and load; a logic set to run every 100 milliseconds, for example, may actually run at intervals longer than 100 milliseconds. Control that needs more precise timing requires a real-time operating system (RTOS). For the execution interval setting, see Part 4.