Lora Rpi Pico
Lora Rpi Pico
with the
Raspberry Pi Pico
Fun with MicroPython
Using a Raspberry Pi Pico with MicroPython The Raspberry Pi Pico (Figure 1) is a new microcontroller board, featur-
is a beginner-friendly way to program. ing the RP2040. This chip is designed and developed in-house by the
Raspberry Pi Foundation and therefore it is their first silicon. After seeing
Adding a SeeedStudio RFM95 LoRa all the news around it — and after hearing about some of the doubts and
module and a DS18B20 temperature sensor fascination with it — I figured it was time to get some projects started.
can create a quick LoRaWAN node with The board offers no Wi-Fi but can be run on a set of batteries for porta-
ble applications, so why not combine the Raspberry Pi Pico with a LoRa
MicroPython. And as putting components modem? The HOPERF RFM95 (Figure 2) is a well-priced and widely
only on breadboards may be a bit instable, a supported module that has been used in former Elektor projects, like
suggestion for a PCB is included. My First LoRaWAN [1]. While in this article an STM32 BluePill module
was used, this time we will go with the Raspberry Pi Pico.
What we will build is a simple temperature measurement device using
a DS18B20 sensor that will transmit data to a LoRaWAN gateway which
will forward the data to The Things Network Cloud. From there we
will grab the data with a (classical) Raspberry Pi running the (home)
automation platform Node-RED.
We will build the temperature sensing LoRa node on a breadboard, but if you
prefer a PCB instead, this article will provide a draft one, done with KiCad.
Figure 3 gives you the dataflow from the Raspberry Pi Pico over the
LoRaWAN to The Things Network server. As you can see, a gateway is
in the middle, translating the RF data into something that can be trans-
ported over the Internet to The Things Network. The data delivered to
Figure 1: The Raspberry Pi Pico.
the The Things Network shall be processed as we are interested in the
temperature transmitted. To do so, we need to grab this data and are
going to present it on a small web page. Figure 4 shows the dataflow
from The Things Network to a Raspberry Pi that will generate a web
page with the actual transmitted temperatur and also a chart indicating
the last transmitted values.
The software used to grab the data form The Things Network and gener-
ate a web page is Node-RED. Node-RED is a tool that allows to build
graphically data flows and process various data. This can be done just
Figure 3: Dataflow to The Things Network. Figure 4: Dataflow from The Things Network to User web page.
+3V3 +3V3
Required Material J1 J3
C1 C2 R5 20 1 40 1
> Raspberry Pi Pico *
USB
GP0 VBUS V_BUS
19 2 39 2
4k7
V_SYS
> RFM95 *
GP2 VSYS
4µ7 100n 18 3 38 3
10V GND LED GND
> Bread board set * 17 4
GP2 3V3_EN
37 4
BOOTSEL
2 14 7 34 7
MISO GP5 GP28
> 2x20 pin header + 2x8 pin header NSS
5 13 8
GND MOD1 GND
33 8
868S2 DIO1
15 9 12
GP9 GP22
29 12
16 8 13 28 13
Parts marked with * can be ordered
Raspberry Pi PICO
Products. DIO5
7 5 16
GP12 GP19
25 16
4 17 24 17 R11
GND
GND
GND
3 18
GP13 GP18
23 18 U2
4k7
1 8 10 GND GND
For the breakout board Gerberfiles 2 19 22 19 3
GP14 GP17
are provided [15] at your preferred
SWCLK
1 20 21 20 2
SWDIO
GP15 GP16
GND
1
PCB service.
Figure 5: Schematic how to attach 210047-003 DS18B20
components.
Figure 9: Thonny
installed in version
3.3.4.
can be either an internal one, provided by the GPIO block inside your of the TinyLoRa library for CircuitPython by Adafruit. The limitations
MCU, or an external one. To have a reliable data transfer an external mentioned are that we can only transmit data, but will be unable to
pull-up resistor is prefered. receive anything back from the LoRaWAN. With this limitation, we also
needed to use APB (activation by personalization) for authentication,
If all the hardware is set up, it will look like what you see in Figure 6, meaning that the network session key and application session key
so we can start coding. are stored inside our code.
MicroPython and LoRa As this is an example to show that you can do LoRa with MicroPython
As using C/C++ for LoRa has been shown in other projects, this time on the Raspberry Pi Pico, it is not perfect, but it can be done quickly
we are going to choose MicroPython. As the Raspberry Pi Pico is a to get started. The modified files needed can be downloaded from the
well-suited board for education, using MicroPython will make things Elektor GitHub page [12]. Also note that even if seems to be working
even a bit easier. If you are new to MicroPython on the Raspberry software, stability might be an issue that needs to be addressed at
Pi Pico, take a look at Gareth Halfacree and Ben Everard’s book, some point.
Get Started with MicroPython on Raspberry Pi Pico (Raspberry Pi
Foundation, 2021). If you would like to get a paper copy, head over Getting MicroPython on the Raspberry Pi Pico
to the Elektor store [5]. If you would prefer an E-Book, grab a free The Raspberry Pi Pico comes with an included bootloader that makes
English copy at [6]. it easy to swap the running firmware on it. As we are going to use
MicroPython, we will install the latest version available from [8] as you
Using LoRa and MicroPython has been done on other platforms but can see in Figure 7. Download the UF2 file form the webpage and
introduces some addtional challanges. MicroPython is an interpreted prepare your Raspberry Pi Pico to enter bootloader mode.
language like BASIC was on the C64 or other home computers. While
this allows easy access, it leads to some CPU overhead during execu- To enter bootloader mode, disconnect the Raspberry Pi Pico, press
tion and thus some limitations. the BOOTSEL button and reconnect it to your computer. It shall now
a new mass storage device show up, like you see in Figure 8. Drag
The library we are going to use is a slightly modified version of uLora the downloaded UF2 file into this drive, so MicroPython in its current
by fantasticdonkey found on GitHub at [7]. This library itself is a fork version will be installed.
Figure 11:
Flowchart
for data
transmission.
You may notice in the code that a value is written to a file called current. over to the version 3 stack using [10]. After the login to our account,
txt. This one will store the current frame counter we are using for trans- we need to create a new node. To create a new node, we first need to
mission to The Things Network.If we would not save the value, the value setup an application. As Figure 13 shows, you need to click on Create
would be zero again at every restart of the node, e.g. after a battery an application and follow the user dialog shown in Figure 14. Select
change. If the counter would be reset every time the device is rebooted, the owner of this application, in this case your account, and fill in the
the transmitted data would be rejected by The Things Network for Application-ID. A click on Create application will complete the process.
security reasons. After every transmission, the file is updated with the
current value used and loaded on next restart. This is not ideal and As we have set up a new application, we can add a new node to it.
will shorten the flash lifetime. If we are writing every 15 minutes a new This new node will provide the credentials we need to enter to the
value, this means 96 writes a day, or 35040 writes every year, so this MicroPython script. In your newly created application, select Add end
is far from perfect and needs improvement. device, to start a wizard to create a new node, as seen in Figure 15.
The wizard, shown in Figure 16, will ask to select a device. As our
To The Things Network and Back device will not be in the list, you need to choose Manually. As seen in
How to setup a LoRa Node has been discussed in previous articles, but Figure 17, you need to choose Activation by personalization (ABP) and
I still want to share a few words about it. The former article showed The select the LoRaWAN version for our MAC. Here you can choose MAC
Things Network stack in version 2. Recently, version 3 was announced, V1.0.2. Select Start to get to the basic settings like shown in Figure 18.
and in some places, you can already transfer your applications and
gateways into the new version 3 stack and therefore console. Enter the End device ID, a unique ID for your node. The field End device
name and End device description are used for your convenience to
To get data transmitted, you need to add a few important parame- later distinguish what nodes you have created. The next page, as seen
ters to the provided MicroPython files. We need to have an account in Figure 19, will require you to select the frequency plan. Set this
at The Things Network to use the communities infrastructure. When according to your region. As the node we are doing just does enough
an account has been created, or you like to create one, you can head LoRaWAN to transmit data, we have no support for Class B or Class C.
For more convenience, a demo flow like shown in Figure 28 has been DIO0 for minimal operation. Figure 3 shows the schematic to attach
set up and will display new arriving data accessible through a web the RFM95 to the Raspberry Pi Pico. Also note that we have added
browser. Figure 29 shows how the webpage looks at the end. Here two capacitors, 100 nF (C2) and 10µF (C1), next to the RFM95. The
you just need to enter your credentials. capacitors shall supply peak currents if the RFM95 is in transmit or
receive mode, as recommended by the datasheet.
Simple and Beginner Friendly
While this is not the most complex project you can do, it will allow you Also, you can see R5, a 4.7 kΩ resistor on the reset line. This one is
to start tinkering around the Raspberry Pi Pico and LoRaWAN. Using normally not required, as the RFM95 offers an internal pull-up resis-
MicroPython can be, especially for beginner, a smooth transition into tor. As the module has been used in a few projects, it showed that
the world of embedded systems. The time required to build and run an external pull-up avoids undesired resets that otherwise happen
this project makes it suitable for (virtual) classrooms and teaching. from time to time without it. Something not used by the MicroPython
But there was a PCB promised at the beginning. If you are just into software but may be later by C/C++ libraries are DIO0, DIO1, DIO2
software, you can stop reading. If you like not only using electronics and DIO3. While DIO0 is required for general operation on most LoRa
but also building them, the next part is for you. libraries, the other DIO ones provide optional features. These features
can be used with other librarys.
A Word of Warning
Usually, if we present schematics and PCBs in Elektor, they have been For the DS18B20, shown in Figure 3, we actively power the VCC pin.
built and tested (at least to the extent that they will not catch fire or kill One-Wire allows for passive powering by just using the data pin and
cute kittens). With this PCB and schematics things are different. The ground (parasitic power). These non-genuine ones behave strangely
PCB and therefore the schematic is a work in progress. While it should when not actively powered via VCC. Therefore, all three pins, including
work, it is not tested, so be aware that there may be some bugs left. VCC, are connected in case one of these fake sensors is accidentally
Also, with this schematic, you can download all KiCad files from the installed. So be careful: if you accidently buy nongenuine DS18B20
Elektor page or the Elektor GitHub repository [12] to modify or change sensors, the performance might suffer, or powering it with just the
it to your own needs. If you seen some design faults or stupid design data pin is not working at all. For One-Wire we need a 4.7 kΩ pull-up
choices (and we do not mean something like using a Raspberry Pi resistor on the data line.
Pico), please feel free to give us your suggestions. This is not limited
to faults. If you missed something that you would like to have included, The Raspberry Pi Pico is a module, so you do not have to connect
feel free to suggest your changes. As I already mentioned, this is a many components. Something that is different are the V_BUS and
work in progress. Your feedback is welcome. V_SYS labels attached to pin 40 and 39. These will be used to attach
a rechargeable battery to the build and will bring us to part two of
Schematic: Part 1 the schematic.
The schematics are divided into two parts, even if all components are
on one KiCad sheet. The first part is just connectivity for the RFM95, our Schematic: Part 2
LoRa transceiver, and a DS18B20 temperature sensor to the Raspberry The second part of the schematic, shown in Figure 30, contains the
Pi Pico. For the RFM95, we need an SPI connection, consisting of section for a rechargeable battery. This consists of an MCP73871-
MISO, MOSI, SCK and nCS. In addition, we need to add RESET and 1CC lithium charger with power path. Thus, if we supply power to its
GND
R18 R21
J4
0Ω
0Ω
1 2 3
R20
0Ω
R9 R13
5 4 3 2 1 10 9 8 7
120k
470k
PROG2
OUT
OUT
OUT
SEL
IN
IN
THERM
VPCC
6 20 11 6
/PG OUT IN OUT
R4 R3 7 19 12 IC2 5
STAST2 IC1 IN IN OUT
8 18 13 4
100k
10k
27k2
10 16 15 2
VSS
PROG3
VBAT_SENSE 1k OVP PGOOD TP3
PROG1
D3 D1 D2 16 1
GND DEVSLP
VBAT
VBAT
C4
VSS
IMON
dVdT
/FLT
ILIM
11 12 13 14 15 1 8 100n
VM GND
2 7 17 18 19 20
VM IC3 GND
3 6
R8 R6 R7 R19 R2 R1 VM VDD R16 R17 R12 R14
4
VM
XB8089D GND
5 C3
100k
33k2
220k
47k
10k
470Ω
470Ω
470Ω
0Ω
1k
EP_(GND)
1n5
J2
2 1
Figure 30: Battery charger and protection. TP1 TP2 210047-029
input, it will pass this to its output, and not use the battery at all. If the for the DC/DC converter, on the Raspberry Pi Pico. We use R17 with
external power is disconnected or insufficient, an automatic switch 47 kΩ to limit the current at 1.89 A, as this is the maximum the lithium
to the battery will happen. The charger is currently set up to charge charger will be able to supply. Why it is a bad idea to use a polyfuse?
batteries with 1000 mA. Read the Elektor article [13]. Using a diode would introduce at least
0.3 V drop across it, meaning we are converting energy into heat.
To protect the attached battery an XB8089D, already used in the
GreatScott! DIY LiPo Supercharger Kit [14], is included. This will prevent Something not often seen in PCBs are R20, R21 and R18. Those are
over-charge, over-discharge, over-current and reverse polarity. The 0 Ω resistors that will allow you to disconnect parts of the battery
last element is a Texas Instruments TPS25940 eFuse. This one acts charger. If some parts need closer investigation or don’t work at all,
as ideal diode, preventing a current flow back to the lithium battery they can be separated and bypassed without breaking traces on the
charger. It will serve as an over-current and over-discharge protection PCB. Just the removal of some SMD components would be sufficient.
element. Overvoltage und undervoltage are determined by R9, R10
and R16. The datasheet provides the required formulas to calculate A Quick Placement on a PCB
the desired values. We use 120 kΩ for R9, 27.2 kΩ for R10 and 33.2 kΩ As the schematic is done, all parts have been placed on a PCB and
for R16 to get 5.37 V for over voltage lockout and 2.957 V for under- are routed. It is not looking nice for now, but for a first try, it will server
voltage lockout. This should be in the safe range, for the battery and the purpose. Figure 31 shows the first rendering. Figure 32 gives an
WEB LINKS
[1] My First LoRaWAN: www.elektormagazine.com/magazine/elektor-141/57159
[2] Project files on GitHub: https://github.com/ElektorLabs/210047-LoRa-with-the-Raspberry-Pi-Pico
[3] Dragino LPS8 Indoor LoRaWAN Gateway: www.elektor.com/dragino-lps8-indoor-lorawan-gateway
Dragino PG1301 LoRaWAN GPS Concentrator for Raspberry Pi: www.elektor.com/dragino-pg1301
[4]
Get Started with MicroPython on Raspberry Pi Pico:
[5]
www.elektor.com/get-started-with-micropython-on-raspberry-pi-pico
Free Get Started with MicroPython on Raspberry Pi Pico PDF (EN):
[6]
https://hackspace.raspberrypi.org/books/micropython-pico/pdf/download
[7] GitHub repository of uLoRa: https://github.com/fantasticdonkey/uLoRa
[8] Raspberry Pi Pico MicroPython: www.raspberrypi.org/documentation/pico/getting-started/
[9] Thonny IDE download: https://thonny.org/
[10] The Things Network v3 Stack: https://eu1.cloud.thethings.network/console/
[11] Node-RED installation instructions: https://nodered.org/docs/getting-started/raspberrypi
[12] Elektor GitHub: https://github.com/ElektorLabs
[13] DIY LiPo Supercharger Bundle: www.elektormagazine.de/articles/diy-lipo-supercharger-bundle
[14] GreatScott! DIY LiPo Supercharger Kit: www.elektormagazine.com/191188-B-01
[15] RFM95 BOB Gerberfiles: https://github.com/ElektorLabs/191069-RFM95-BOB/