The Raspberry Pi is a portable computer with an onboard processor that fits comfortably in the palm of your hand. Compared with general purpose computers, it’s an affordable option developed by the Raspberry Pi Foundation.
The Raspberry Pi Model B was introduced in 2012 as the first sellable unit, and the company has since released many more models. There are even low-cost models like the Raspberry Pi Zero Series, which is quite small and tailored to embedded systems applications. All the models operate on an operating system called the Raspberry Pi OS, a Linux flavor niched for Raspberry PI Computers.
In this tutorial, we’ll be using the Raspberry Pi 4 Model for a headless setup through a SSH connection using Visual Studio Code (VS Code). The Raspberry Pi 4 Model has a Quad-core ARM Cortex-A72 (64-bit) SoC at 1.5GHz, up to 8GB RAM options, video inputs, Ethernet shield, USB ports, MicroSD card slot for storage, USB-C power input and 40 General Purpose Inputs and Outputs Pins (GPIO). Impressive, right?
You’ll be able to use the Raspberry Pi as a personal computer, for home automation and IoT projects, robotics projects, network applications, educational tools, and Artificial Intelligence projects.
Table of Contents
Understanding the Headless Setup
Many Raspberry Pi computers are sold with additional peripherals, including a keyboard, mouse, and monitor, that are essential for the Raspberry Pi’s setup. A headless setup is the process of configuring the Raspberry Pi or preparing it for use without needing these peripherals. This entails operating the Raspberry Pi through a network protocol like SSH (Secure Shell) or VNC (Virtual Network Computing).
This is really helpful when you don’t need peripherals, as it lets you use your personal computer to connect to the Raspberry Pi without needing to purchase specialized peripherals. It’s also excellent for remote access. This headless setup is also essential for remote monitoring systems, such as surveillance systems with remote camera access, and IoT systems.
Remote development lets you write code and modify your Raspberry Pi and other devices connected to the GPIO pins through a headless configuration via SSH.
SSH guarantees a secure connection for transferring and modifying files, as well as transferring and debugging commands from one computer (your personal computer) to another computer (the Raspberry Pi). It restricts unauthorized access from any other system that aims to intercept the communication channel.
Prerequisites
Here’s what you’ll need to follow along with this tutorial:
Hardware Requirements
-
Raspberry Pi 4 or 5
-
MicroSD Card (8GB or higher recommended)
-
Flash Drive with SD Card Slot or a MicroSD Card Adapter
-
Power Supply (5V 2A/3A)
-
Network Connection (Wi-Fi, Pi, and laptop must be on the same network)
-
Personal Computer (Windows, macOS, Linux)
Software Requirements
-
Raspberry Pi Operating System (Raspberry Pi OS)
-
Visual Studio Code
-
Remote SSH Extension in VS Code
Preparing the MicroSD Card
The Raspberry Pi requires a MicroSD Card that serves as the storage of your the Raspberry Pi OS using Raspberry Pi Imager. The operating system of the Raspberry Pi provides a graphical interface to interact with the Raspberry Pi, store files and datasets, and write commands to get your Raspberry Pi working.
But the Raspberry Pi needs an empty MicroSD Card to install the Raspberry Pi OS in the MicroSD Card. Here are some step by step instructions that’ll show you how to get your MicroSD Card setup before inserting it back into the Raspberry Pi for SSH Connection.
Downloading and flashing Raspberry Pi OS
Insert your MicroSD Card into a flash drive with a SD Card slot
Aside from using a flash drive with an SD Card slot (so as to get the memory card connected to the computer), you can also use a SD Card adapter. Make sure it’s inserted into your computer where you have the Raspberry PI Imager downloaded to flash – that is, transfer the OS into the SD Card.
Download the Raspberry Pi Imager based on your PC’s operating system
This involves clicking the link and selecting your operating system (either MacOS, Windows or Linux operating system). The Raspberry Pi OS comes in these variants for different OSes
Next, install and open the Raspberry Pi imager
Click the Raspberry Pi Imager download, follow all the instructions during the installation process. Once this screen pops up, you’re good to go!
Choose your Raspberry Pi Device and operating system and select Storage
For each of the three configurations, you must select one sequentially. Select a device according to the type of Raspberry Pi you have, and various options will appear. I selected the Raspberry Pi 4, as it is my preferred device. You may choose between the Raspberry Pi 5 and the Raspberry Pi Zero 2 W, depending on your device requirements.
Next, proceed to the operating system – I would recommend choosing the 64-bit version. While many people opt for the legacy version (32-bit), I think the 64-bit version is best. Once you’re finished, you can choose a storage option, and your MicroSD should appear. My storage is around 128GB, which is why you can see 125.1GB displayed there in the screenshot below:
Click on “Next” and edit the settings
It is a customary practice to keep your username as “pi”, but it’s not required. The goal is to have something simple and easy to remember when setting up your SSH connection. It’s also helpful to make your password simple. I used ‘roboticsai’.
Try to avoid using numbers simply to make things easier, because you may not be able to see what you are entering in the terminal. Then, make sure that your wireless LAN and SSID (WIFI or Hotspot name if you’re using a phone, as well as the password for your WIFI or Hotspot) is the same network as the one linked to your computer.
Click on “SERVICES” and enable SSH. Then use password authentication for security and Click on “SAVE”.
After you’ve completed the changes in the General Section, go to the Services section and click the checkbox button “Enable SSH”. Once highlighted, make sure you pick “Use password authentication”, avoid the “RUN SSH-KEYGEN” button at the moment, and then click Save.
Click “YES” to apply the customizations, and the Raspberry Pi OS should get flashed into your SD Card.
Following the previous stage, you will be shown various buttons to apply the adjustments you have made. Pick yes, and the Raspberry Pi OS will be flashed or transferred to your Memory Card. This could take between 10 and 20 minutes to go from transferring to writing or customizing. Hold on and enjoy the process.
After a successful installation into the disk, remove your SD Card.
You will receive a successful popup like the one shown below. This demonstrates that all processes were completed successfully, and the Raspberry Pi OS is now installed.
How to Boot the Raspberry Pi
Eject the MicroSD safely from your computer
Once the installation is successful, eject the MicroSD safely from the computer.
Insert it “upside down” into the MicroSD card slot of your Raspberry Pi
To properly insert the MicroSD card, place it gently into the slot with the back or gold side facing upward. It will protrude slightly once it is inserted. You are good to go!
Connect the USB-C port of your Raspberry Pi to your computer. Give the Raspberry Pi some time to load
Get a USB-C cable and connect one end to your Raspberry Pi’s USB-C port and the other to a laptop port. It should light up red, indicating that there is an adequate power source. You may also power your Raspberry Pi directly by plugging into a wall socket.
After a while, the memory card should begin to boot into the Raspberry Pi, and the green LED will blink for a while. In the next section, we’ll talk about the different states of the two LEDs during and after a successful boot.
Understanding the LED Status of the Raspberry Pi During Setup
The below table describes the LED statuses you might see when you power on your Raspberry Pi and the SD Card is in the slot.
LED Color | State/Pattern | Meaning/Recommendation |
🔴Red | Solid (ON) | Stable and sufficient power supply |
🔴Red | Off or Blinking | Undervoltage detected (Use a direct phone Charger connected to a Socket) |
🟢Green | Blinking (Irregular Pattern) | SD Card is being read/written (normal booting activity) |
🟢Green | Solid (ON) | Raspberry Pi is stuck or trying to boot. |
🟢Green | Off | No SD Card detected or boot completed |
🟢Green | Repeated blink patterns (for example 4 long, 4 short) | Error code indicating firmware issues. |
🟢Green | Constant Blinking | Normal activity (Raspbian OS is loading and running smoothly) |
How to Establish an SSH Connection
The SSH (Secure Shell) connection is a network protocol that allows two computers to safely communicate without leaking any information. It’s also used for remote command line execution and for file transfers between two computers.
To establish an SSH connection, you’ll have to complete a few steps. Then I’ll explain how to enable SSH using a Visual Studio Code extension
Create a wpa_supplicant.conf.txt
in the same folder of your Raspberry Pi SD Card
Insert your MicroSD card back into the computer. Then the files that comprise the Raspberry Pi OS will appear on your computer. Create a new text (.txt) document on your computer, similar to the image below, under the SD Card storage section.
Add the code below, making sure that “ssid” is the name of your Wi-Fi network and “psk” is your network’s password.
country=NG # Your 2-digit country code
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network=
ssid="Josiah"
psk="roboticsai"
key_mgmt=WPA-PSK
Save the file on the same SD Card
Once you’ve finished producing the text file, save it to the SD Card storage, as shown in the image below.
Create a .ssh folder
In your personal computer, create a .ssh
folder if it doesn’t exist on your personal computer.
If it exists, the .ssh
folder should contain files like id_rsa
, known_hosts
, and config
files. It shouldn’t be empty.
After a successful boot, open your terminal or command line application on your personal computer.
Make sure that the Raspberry Pi is connected to the same network before moving ahead. Once your wifi or mobile hotspot is switched on, make sure it’s the same password as the wpa_supplicant.conf.txt
and the settings page while installing the Raspberry Pi.
As long as the SD card is in the Raspberry Pi and there is adequate power supply for at least 2-5 minutes, the Raspberry Pi will get connected to the wifi or your mobile hotspot.
How to Resolve Connection Problems
If there is no connection, reinstall the Raspberry Pi OS Imager on the SD Card again. Then you can also change the network AP Band from 5GHz to 2.5GHz or vice-versa. This can be very tricky.
It should get connected after trying this. Just make sure that the passwords are consistent and that you don’t accidentally have the caps lock key switched on while typing, for example.
To confirm if the Raspberry Pi is connected using the command line interface, use the ping
command – it shows the devices connected to the device.
ping raspberrypi.local
After running the above command, you should see an image showing the connection once it’s successful like this:
For establishing an SSH connection using the terminal, run the code below:
ssh pi@raspberrypi.local
This will result in a request for a password. If it shows an error like the image below, it means you have to delete the known_hosts.old
and known_hosts
if either or both exist in the .ssh
folder in your PC. This is because the keys are conflicting with each other. Then re-run the above code ssh pi@raspberrypi.local
in your terminal.
After successful entry, type “yes
” in the terminal.
Connection Closed
should show when the connection is successful.
How to Set Up Visual Studio Code for Remote Development
Download and install Visual Studio Code if you don’t have it already.
Then, click on the VS Code extension and search for Remote - SSH
by Microsoft and install it to your machine.
Next, click on the “Remote Explorer” icon that looks like a monitor. Select the SSH config in your C:\Users\name\.ssh\config
folder.
Make sure the config has this command:
Host raspberrypi.local
HostName raspberrypi.local
User pi
Enter your username as raspberrypi.local
and input your password – the same as the password during loading Raspbian OS.
After inputting the correct password, it should start downloading the server.
Congratulations! The image below has a blue rectangle button showing SSH:raspberrypi.local
which shows a successful SSH Connection through Visual Studio Code. This also means you can start remote development as we discussed earlier in this tutorial.
How to Write and Run the Code Remotely
Create a new file on your VS Code. This way, you’re creating files and writing to them directly. Go to the terminal and type the commands to create a folder and a file:
Create a new file and write in your code
Create a new file and name it led.py
on your Visual Studio Code. It should be in the same folder as test-raspberry
on the Raspberry Pi remote network through the SSH connection on VSCode.
Once you have your file created, you can write in your code such as blinking LED to a Raspberry Pi, as you can see in the code below:
from gpiozero import LED
from time import sleep
led = LED(17)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
After writing this code in the new file you’ve created, run the code by typing the command below in your terminal:
python led.py
As soon as this command is sent, the LED positive terminal is connected to the GPIO 17 according to the code and the negative terminal is connected to the GND GPIO pin of the Raspberry Pi. The image from Random Nerd Tutorials below shows the GPIO pins and their number to understand the connection. Just note that the connection of the LED is beyond the scope of this tutorial.
The LED should start blinking each second according to the code. With this, you can now control your Raspberry Pi (a tiny computer) with another computer (your personal computer) through an SSH connection on Visual Studio Code.
Conclusion
In this tutorial, you went through the whole process of setting up a headless Raspberry Pi for remote development using VS Code.
This offers a wide range of benefits: there’s no need for external peripherals, it provides remote access from anywhere within your network, and it leverages efficient coding and debugging with VS Code integration.
You can use this to deploy web servers and IoT dashboards, and you can explore with automating processes using Python scripts and GPIO control.