Third party cookies may be stored when visiting this site. Please see the cookie information.

PenguinTutor YouTube Channel

Badger 2040W - Hackable ID badge with Raspberry Pi Pico microcontroller and Wireless

The Badger 2040W is an ID badge powered by a Raspberry Pi Pico. It is an upgraded version of the Badger 2040. I've already covered that in my guide to the Badger 2040 and creating Tic-Tac-Toe.

The one feature that I thought was missing from the Badger 2040 was wireless. Clearly Pimoroni thought so too as they created a wireless version of the Badger 2040 called the Badger 2040 W. The Badger 2040 W uses a Raspberry Pi Pico W soldered to the back using the castellations on the Pico.

In this video below I provide an introduction to Bager 2040 W. It includes a simple program created using MicroPython which uses the built in wireless to connect to my Pixel Server running on a Raspberry Pi.

Please Subscribe and click the bell icon to be notified of my future videos.

You can purchase from Pimoroni: Badger 2040 W

Programming the Badger 2040W

The Pimoroni Github repository includes examples in C++ and MicroPython. My examples below are based on MicroPython.

There are some changes compared with the Badger 2040, which are explained in the Badger 2040W library README.md file.

Loading the libraries and creating the display on the Badger 2040W

The first thing is to load the appropriate libraries. These are different libraries to the Badger 2040, but can be loaded in such a way that they use a similar name.



import badger2040w as badger2040

from badger2040w import WIDTH, HEIGHT

display = badger2040.Badger2040W()

The last line creates "display" as an instance of the Badger2040W class. Although this is called display it's also used for the LED and the network connectivity.

Programming WiFi on the Badger 2040W

The Badger 2040W has simple network configuration. It first needs to be configured through the wifi configuration file (WIFI_CONFIG.py) and can then be called through the code.

To establish a connection with the local WiFi network and setup the IP address using DHCP then call the command:
display.connect()

This uses the display object created previously, but is making use of the network feature of the Pico. It does also update the display in that during the connection attempt it says "Connecting" and when the connection is established to the router and the IP address provided then it displays that on the screen. Once the connection is successfully made then it returns control to the program.

Once connected to the network then you can make use of the urequests library to perform HTTP get requests. For example:



    url_string = create_url()

    print ("Loading {}".format(url_string))

    r = urequests.get(url_string)

    print (r.content)

    r.close()

This makes use of a function I use to create a url, but you can replace the first line with a fixed http string if preferred. This includes some logging showing the url to be loaded, performing the urequests get command and then printing out the contents. It then closes the connection.

Programming the Display on the Badger 2040W

The display is an e-ink display. There are libraries built into the Pimoroni image which makes it easy to program the display. Whilst simple it is a little basic but much of that can be dealt with during your own code. For example Creating circles on the Badger 2040. I've also created my own menu system which is explained later in this page.

In my first example I created a very simple interface which just uses a text menu indicating which button to press for each option. This is done by using the display object (created earlier):



display.set_font("bitmap8")

display.text("Text string", 2, 80, WIDTH - 105, 2)

The first line is to set the font, the second is to display some text. To display text needs the text as well as the location and size of a text box to place the text.

To change the colour (Black, White or dithered grey) then the set_pen() method needs to be called with a value from 0 to 15 (white to black).

It is also possible to draw lines using the lines() method.

Programming with the button switches

The buttons are read using the standard MicroPyhon commands in the machine library. The following commands use the built-in pin values to setup as buttons.



button_a = machine.Pin(badger2040.BUTTON_A, machine.Pin.IN, machine.Pin.PULL_DOWN)

button_b = machine.Pin(badger2040.BUTTON_B, machine.Pin.IN, machine.Pin.PULL_DOWN)

button_c = machine.Pin(badger2040.BUTTON_C, machine.Pin.IN, machine.Pin.PULL_DOWN)

button_down = machine.Pin(badger2040.BUTTON_DOWN, machine.Pin.IN, machine.Pin.PULL_DOWN)

button_up = machine.Pin(badger2040.BUTTON_UP, machine.Pin.IN, machine.Pin.PULL_DOWN)

Basic web access demonstration

This is the source code for a simple example which uses the features listed above. For more details see the menu system later on this page.



import badger2040w as badger2040

from badger2040w import WIDTH

import machine

import urequests

import json





# Test server address

SERVER_ADDR = "http://192.168.0.43"



pixel_options = [

    ["All on", "/set?seq=allon&delay=900&reverse=0&colors=ffffff%2C202020%2C202020"],

    ["Chaser", "/set?seq=chaser&delay=900&reverse=0&colors=ffffff%2C202020%2C202020"],

    ["All off", "/set?seq=alloff&delay=900&reverse=0&colors=000000"]

    ]



# Display Setup

display = badger2040.Badger2040W()

display.led(128)

display.set_update_speed(2)



# Setup buttons

button_a = machine.Pin(badger2040.BUTTON_A, machine.Pin.IN, machine.Pin.PULL_DOWN)

button_b = machine.Pin(badger2040.BUTTON_B, machine.Pin.IN, machine.Pin.PULL_DOWN)

button_c = machine.Pin(badger2040.BUTTON_C, machine.Pin.IN, machine.Pin.PULL_DOWN)

button_down = machine.Pin(badger2040.BUTTON_DOWN, machine.Pin.IN, machine.Pin.PULL_DOWN)

button_up = machine.Pin(badger2040.BUTTON_UP, machine.Pin.IN, machine.Pin.PULL_DOWN)



# Connects to the wireless network. Ensure you have entered your details in WIFI_CONFIG.py :).

display.connect()





def draw_page():



    # Clear the display

    display.set_pen(15)

    display.clear()

    display.set_pen(0)



    # Draw the page header

    display.set_font("bitmap6")

    display.set_pen(0)

    display.rectangle(0, 0, WIDTH, 20)

    display.set_pen(15)

    display.text("Pixel Server", 3, 4)



    display.set_pen(0)

    display.set_font("bitmap8")



    # Draw articles from the feed if they're available.

    display.text("a: " + pixel_options[0][0], 2, 40, WIDTH - 105, 2)

    display.text("b: " + pixel_options[1][0], 2, 60, WIDTH - 105, 2)

    display.text("c: " + pixel_options[2][0], 2, 80, WIDTH - 105, 2)





    display.update()





draw_page()



while 1:



    if button_down.value():

        pass



    if button_up.value():

        pass



    if button_a.value():

        url_string = SERVER_ADDR+pixel_options[0][1]

        print ("Loading {}".format(url_string))

        r = urequests.get(url_string)

        print (r.content)

        r.close()



    if button_b.value():

        url_string = SERVER_ADDR+pixel_options[1][1]

        print ("Loading {}".format(url_string))

        r = urequests.get(url_string)

        print (r.content)

        r.close()



    if button_c.value():

        url_string = SERVER_ADDR+pixel_options[2][1]

        print ("Loading {}".format(url_string))

        r = urequests.get(url_string)

        print (r.content)

        r.close()



Creating the Pixel Client Menu System for the Badger 2040W

To support the pixel client for the Badger 2040W I first wrote a simple menu system. This means that it is to adapt to other projects in the future. The menu system is designed using object-oriented programming based around a class I created called BadgerMenu. The menu system is very basic at the moment. I have included support towards expanding this to include images but that is not currently implemented.

Badger 2040W WiFi enabled conference ID badge - with Pixel server menu system

The client is fully functional and can configure various aspects of the Pixel Server, including changing the sequence, setting the colours and setting the delay value (speed of the sequence).

Source code

The source code for the Pixel Client is available on Github. See the Readme.md file for details of how to install on the Badger 2040 W.

For a further explanation about the menu system see: Programming a GUI menu for an E-Ink display - Badger 2040W.

Related projects

Also see:

Future projects

For the latest updates please:
Subscribe to the PenguinTutor YouTube Channel
and
Follow @penguintutor on Twitter

Previous Hackable ID Badge with Game
Hackable ID Badge with Game
Next Project list
Project list