Getting Started with Raspberry Pi Zero W and Python 3


The small size and connectivity aspects of Raspberry Pi Zero W makes it a great candidate for IoT Hub scenarios. It can be easily programmed using Python, so it has great potential. This project primarily explains how to do the headless installation of Raspbian OS and Python using Windows and VS Code. It will get you started.

Setting up Raspberry Pi Zero W

Download Raspbian Buster Lite for headless install. If you want desktop version, feel free to choose another version.

  • When the App starts, choose Raspbian Lite as OS for headless install.
  • Choose SD card drive and start the install.
  • When the install is complete, you may have to remove and re-insert the SD card.

Headless Pi can be accessed using SSH via WiFi.

  • To enable SSH, create an empty file named ssh in the root folder. Note: No extension
  • For WiFi connectivity create wpa_supplicant.conf file in the root folder with the following contents adjusted as per your WiFi.
  • Note that it must have Linux style line ending. In VS code, it can be done by changing CRLF to LF in the right bottom.
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev


Apart from SSID and password, the file may need some other changes. Here are some tips:

  • Login into your WiFi portal to see if it is of type WPA-PSK or WPA2-PSK
  • If it is of WPA2-PSK, set
    Check how encryption is done in the portal. If it is AES, add the following line before key_mgmt
  • If you would like to share more tips, please add them to the comments.

Please save the files locally too. As the wpa_supplicant.conf file may needing tweaking. And after you plug the SD card in the Raspberry Pi Zero and again in the PC, you might find that the files that you created have disappeared. That is normal.

  • Plug your SD card in the Raspberry Pi and connect it to your PC using USB cable. It takes several minutes for it to boot.

Open Terminal in VS Code with Powershell

  • Type command ssh pi@raspberrypi.local
  • If you get response like unable to find host, some troubleshooting tips are listed above.
  • By default the user name is pi and password is raspberry
  • If you are successful at the login, initial installation and connection to Wi-Fi is successful šŸ™‚

Installing Python 3

Before installing Python, you may want to make sure that the Raspbian OS is up to date. To get the latest update, run the following command:

  • sudo apt-get update

Raspbian comes with Python 2.7.xx installed (as of this writing). You should uninstall that and install Python 3. To uninstall Python 2.7.xx, run the following commands:

  • sudo apt-get remove python
  • sudo apt autoremove

Now start installing Python 3 and the required packages

  • sudo apt-get install python3
  • sudo apt-get install python3-rpi.gpio
  • sudo apt-get install python3-smbus

Now you are ready to program Raspberry Pi Zero W. Before you connect your circuit, shutdown the Pi from SSH using the following command and disconnect the USB.

  • sudo shutdown

Build PIR Sensor project

This is a small project that shows communication with GPIO port and prints a message. It consist of a PIR motion sensor that detects if a human is in front of it. Based on that it sets the port to High when something is in its vicinity and to low when nothing is near by. The program will print 111 or 000 depending upon the state of the port. (Why take trouble to turn LED on/off, if you can see the print message?)

Connect the circuit as follows:

It is assumed that you are familiar with Python. In the program, GPIO ports are declared using BCM convention and are initialized using RPI library. The loop monitors the port with 1 second interval and prints corresponding message. Here is the code:

# import library for GPIO
import RPi.GPIO as GPIO
# print GPIO info

# time library
import time

# Port of PIR
pir_port = 14

# Setup GPIO mode to Broadcom

# configure the pin as input pin
GPIO.setup(pir_port,  GPIO.IN)

# do the loop
    while (True):
        # if the input is zero, nobody is there at the sensor
        if GPIO.input(pir_port) == 0:
            # found somebody at the sensor
        # wait for 1 sec whether you detect someone or not
# If user presses ^C cleanup the GPIO
except KeyboardInterrupt:

This code can be saved locally on your windows machine and transferred to Raspberry Pi. Transfer the file to Raspberry Pi using WinSCP. (Alternatively, the file can be edited in Raspberry Pi using nano editor.)

After the transfer, go to VS Code -> Terminal, SSH into Raspberry Pi.

With ls command you will see the transferred file there. Now run the following command:

  • python3

You will see 000 and 111 being printed based on what is in front of the PIR sensor.

Hope this gets you started on Raspberry Pi Zero W with Python 3. Please share the post to spread the word.

PS: Thanks to Brother Hackathon for providing Raspberry Pi Zero W.


Published by: Sameer Khandekar

I am a passionate software engineer who loves to work on Azure microservices, REST API, SDKs, and .NET apps using WPF, Xamarin, and MAUI. The work includes highly scalable geo-distributed services and an authentication library with nearly 500 million downloads. I also had fun integrating with hardware using Bluetooth (BLE). More here:

Categories IOT, IOT Hub, Python, Raspberry Pi1 Comment

One thought on “Getting Started with Raspberry Pi Zero W and Python 3”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s