Skip to content

Commit 2f0f974

Browse files
committed
Add article about serial console on a RaspberryPi
1 parent 023e086 commit 2f0f974

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
---
2+
title: Serial Console over USB on a Raspberry Pi Zero 2 W on Windows
3+
description: How to access the console of a Raspberry Pi Zero 2 W over USB on Windows
4+
date: 2025-12-10
5+
topics:
6+
- raspberry-pi
7+
- usb
8+
- linux
9+
- debian
10+
- windows
11+
---
12+
13+
This article shows how to access the console of a [Raspberry Pi Zero 2 W](https://www.raspberrypi.com/products/raspberry-pi-zero-2-w/) over an USB cable. No Wi-Fi, keyboard, or monitor required.
14+
15+
I will show this on Windows. I've tested this with Raspberry Pi OS based on Debian 13.
16+
17+
> [!NOTE]
18+
> This article will change the **USB data port** on your Zero 2 W from host mode to **gadget mode** - meaning the Zero 2 W will present itself as a USB device (think: USB thumb drive) to the computer it's connected to.
19+
>
20+
> This configuration is fully reversible.
21+
22+
> [!NOTE]
23+
> For this guide to work, your Pi needs to support USB OTG (on the go). At the time of writing, this is only supported by Raspberry Pi Zero models but *not* by the regular Pis (like 4 or 5).
24+
25+
## Why would you do this?
26+
27+
Connecting to a Raspberry Pi directly this way is required if you accidentally messed up the network configuration on your Pi and thus can't connect to it via SSH anymore (and you don't have a keyboard and monitor at hand to use instead).
28+
29+
Also, you can use copy and paste from Windows.
30+
31+
My personal use case: Before changing the network configuration of my Pi, make sure I can still access the Pi even if I screw up the network configuration.
32+
33+
## Step 1: Setup
34+
35+
There are two ways to do the next steps:
36+
37+
* If you still have (SSH) access to your Pi, you can do everything directly on the Pi.
38+
* If you don't have access to your Pi, you can insert the Pi's SD card into your Windows PC.
39+
40+
### config.txt
41+
42+
This file is located in the root directory of the SD card - or in `/boot/firmware` on the Pi itself.
43+
44+
Add the following line to the end of the file (in the `[all]` section):
45+
46+
```ini
47+
dtoverlay=dwc2
48+
```
49+
50+
The result will look like this:
51+
52+
```ini
53+
...
54+
55+
[all]
56+
dtoverlay=dwc2
57+
```
58+
59+
#### Details
60+
61+
`dtoverlay` stands for "device tree overlay" and is the Raspberry Pi (or ARM) way of telling the Linux kernel which devices are available. (PCs use ACPI for this instead.)
62+
63+
The overlay `dwc2` is the "dwc2 USB controller driver".
64+
65+
I could not figure out which USB driver is used if the `dwc2` overlay is *not* used. My guess is that this driver only supports USB host mode (i.e. a regular USB port on a computer) whereas you definitely need the `dwc2` driver to run the USB port in gadget mode.
66+
67+
All available overlays can be viewed in the `overlays` folder on the SD card. Check the `README` file in this folder for details.
68+
69+
Note that you can have multiple `dtoverlay` entries and they are cumulative. For more details, see [the official documentation](https://www.raspberrypi.com/documentation/computers/config_txt.html#dtoverlay).
70+
71+
### cmdline.txt
72+
73+
This file is located in the root directory of the SD card - or in `/boot/firmware` on the Pi itself.
74+
75+
Add this *directly* after `rootwait`:
76+
77+
```
78+
modules-load=dwc2,g_serial
79+
```
80+
81+
The result will look something like this:
82+
83+
```
84+
... fsck.repair=yes rootwait modules-load=dwc2,g_serial cfg80211.ieee80211_regdom=DE
85+
```
86+
87+
Note that the whole file must be on a single line.
88+
89+
### Enable TTY
90+
91+
Last, you need to enable and start the `getty@ttyGS0` service on the Pi.
92+
93+
To do this, execute this command on the Pi:
94+
95+
```sh
96+
sudo systemctl enable --now getty@ttyGS0
97+
```
98+
99+
If you don't have access to your Pi, edit the `user-data` file on the SD card and add this `runcmd` section:
100+
101+
```yaml
102+
...
103+
runcmd:
104+
- systemctl enable --now getty@ttyGS0
105+
```
106+
107+
Note that this command will only be executed **on the very first boot** of your Pi. If you've already booted your Pi, edit the `meta-data` file on the SD card and change the value of `instance_id` to something different (like `rpios-image2`). This way, [cloud-init](/cloud-init/user-data.md) (which these files belong to) thinks this is your first boot.
108+
109+
## Step 2: Connect via USB
110+
111+
Next, connect your Pi to your Windows machine via USB cable. Any regular micro USB cable will do.
112+
113+
Plug the cable **into the USB data port** (not the USB power port) of your Pi.
114+
115+
![USB Cable connected to the data port](usb-cable.jpg)
116+
117+
> [!TIP]
118+
> Your Raspberry Pi Zero 2 W will **get its power from the data port** in this case.
119+
120+
## Step 3: Connect from Windows
121+
122+
**Find the COM port (via PowerShell):**
123+
124+
```pwsh
125+
[System.IO.Ports.SerialPort]::GetPortNames()
126+
```
127+
128+
We're using [PuTTY](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html) as the client:
129+
130+
![Configuring COM4 in PuTTY](putty-config.png)
131+
132+
You first need to **switch to connection type Serial**. Then enter the COM port (from above) and `115200` as speed.
133+
134+
That's it.
135+
136+
![PuTTY connect to the Pi](putty-console.png)
137+
138+
> [!NOTE]
139+
> The speed `115200` is the default in modern day Linux systems. On a Pi, it's defined in `/boot/firmware/cmdline.txt`:
140+
>
141+
> ```
142+
> console=serial0,115200 ...
143+
> ```
144+
145+
## Debugging
146+
147+
**Check if TTY service is running:**
148+
149+
```sh
150+
> systemctl status getty@ttyGS0
151+
getty@ttyGS0.service - Getty on ttyGS0
152+
Loaded: loaded (/usr/lib/systemd/system/getty@.service; enabled; preset: enabled)
153+
Active: active (running) since Wed 2025-12-10 15:52:07 CET; 1min 44s ago
154+
```
155+
156+
The service should be `enabled` and `active`.
157+
158+
**If not, check for ttyGS0:**
159+
160+
```sh
161+
> ls -l /dev/ttyGS*
162+
crw-rw---- 1 root dialout 243, 0 Dec 10 15:52 /dev/ttyGS0
163+
```
164+
165+
This file must exist.
166+
167+
**If not, check that the g_serial module configured properly:**
168+
169+
```sh
170+
> dmesg | grep g_serial
171+
[ 0.000000] Kernel command line: ...
172+
[ 0.000000] Unknown kernel command line parameters "modules-load=dwc2,g_serial", will be passed to user space.
173+
[ 2.629195] modules-load=dwc2,g_serial
174+
[ 9.231155] g_serial gadget.0: Gadget Serial v2.4
175+
[ 9.231172] g_serial gadget.0: g_serial ready
176+
[ 9.231184] dwc2 3f980000.usb: bound driver g_serial
177+
```
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)