[{"element_type":"markdown","content":"\u003ch1\u003eRunning Pi-HATs with a Raspberry Pi Pico\u003c/h1\u003e\n\n\u003cp\u003eBeing a Pi-user since the very first Pi1, I own many different\nPi-HATs.  Some of them are in daily use, but many of them are sitting\nin the shelf.  So I wondered if I could give them a second chance in\ncombination with a Pico. And one of my major use-cases are e-ink\ndisplays for the Pi.  These e-inks don't really match with the Pi,\nsince they are optimized for low-power scenarios. But even the Pi-Zero\ndrains batteries too fast to be a suitable partner for these kind of\ndisplays.\u003c/p\u003e\n\n\u003cp\u003eAlthough equipped with a full 2x20 pin socket, most HATs only use a\nfew pins like power, ground, I2C or SPI. So using a bunch of jumper\ncables should already be sufficient. Although that is true and fine\nfor initial tests, a good and solid connection is always the better\nalternative.\u003c/p\u003e\n\n\u003cp\u003eSo I did some research and discovered a few adapter-boards on the\nmarket that might be suitable. But on closer inspection it turned out\nthat most of them missed one important point: just mapping some\narbitrary pins is not enough. So I decided to create my own adapter\nboards.\u003c/p\u003e\n\n\u003ch2\u003eHardware\u003c/h2\u003e\n\n\u003cp\u003eOne board uses the Pi form-factor, the second uses the Pi-Zero\nform-factor:\u003c/p\u003e\n\n\u003cp\u003e\u003cimg src=\"https://github.com/bablokb/pcb-pico-pi-base/blob/main/pico-zero-base.jpg?raw=true\" alt=\"\"\u003e\n\u003cimg src=\"https://github.com/bablokb/pcb-pico-pi-base/blob/main/pico-pi-base.jpg?raw=true\" alt=\"\"\u003e\u003c/p\u003e\n\n\u003cp\u003eThey fit into standard enclosures, but since the USB-connection is on\nthe side they need an additional cutout. Changing available 3D-models\nshould be a simple task. And the bigger adapter PCB has a footprint\nfor a standard JST-2 battery connector exactly where the USB-power\ncutout is. Anyhow, this was not the major challenge when designing\nthese boards.\u003c/p\u003e\n\n\u003cp\u003eThe biggest challenge was the correct pin-mapping. The Pi has I2C,\nUART, two SPI and I2S. I did not take the last one into account so I\nended up with two revisions. I2S needs two consecutive pins. On the\nPi, that is GPIO18 and GPIO19, but they are not next to each other on\nthe pin-header.\u003c/p\u003e\n\n\u003cp\u003eAnother contraint was space. I did not want to route traces below the\nWLAN-chip and antenna of the Pico-W. In the end I had to make a\ncompromise for the Pi-Zero adapter: the first revision maps both SPIs\nbut not I2S, the second revision maps SPI0 and I2S but not SPI1. Which\nis not a big deal since I haven't found a HAT yet that actually uses\nSPI1.\u003c/p\u003e\n\n\u003cp\u003eI also don't map the ID-pins of the Pi. These are used to\nautomatically configure the correct driver on the Pi. On the Pico, you\ndon't run a generic OS but a specific program, so you have to take\ncare about correct drivers already before when you put them on the\nCIRCUITPY-drive.\u003c/p\u003e\n\n\u003cp\u003eThe Pi-adapter has more space. I added a SD-card reader and I broke\nout a number of pins. One of the drawbacks of many HATs is that they\nblock the complete pin-header although they only use a few pins.\nBreaking out the pins is not strictly necessary since you can access\nall pins from the back anyhow.\u003c/p\u003e\n\n\u003ch2\u003eSoftware\u003c/h2\u003e\n\n\u003cp\u003eThe second part of the project was porting the HAT-drivers to the Pico.\nFor Adafruit HATs, that was fairly simple. Adafruit has CircuitPython\nsupport for almost everything they sell. And since Blinka brings\nCircuitPython to the Pi-SBCs, \"porting\" the drivers is a matter of\nusing the correct pins.\u003c/p\u003e\n\n\u003cp\u003eOn example: the speaker-bonnet. The learning guide\n(\u003ca href=\"https://learn.adafruit.com/adafruit-speaker-bonnet-for-raspberry-pi\"\u003ehttps://learn.adafruit.com/adafruit-speaker-bonnet-for-raspberry-pi\u003c/a\u003e)\ntells you it is using the I2S pins GPIO18, GPIO19 and GPIO21 on the Pi.\nAfter looking up the mapping for the adapters, you just plug in those\npins into a small example program provided by a second guide:\n(\u003ca href=\"https://learn.adafruit.com/mp3-playback-rp2040/pico-i2s-mp3\"\u003ehttps://learn.adafruit.com/mp3-playback-rp2040/pico-i2s-mp3\u003c/a\u003e)\nand off you go playing MP3 on the speaker-bonnet. This is actually much\nsimpler on the Pico compared to the Pi, because you don't have to go\nthrough all the steps to install the relevant drivers.\u003c/p\u003e\n\n\u003cp\u003eFor other HATs, you will usually find CircuitPython example code for\nthe Pi using Blinka in the learning guide for the HAT. In this case,\nyou can take the code as is and only replace the pin-numbers.\u003c/p\u003e\n\n\u003cp\u003eI also own a number of HATs from Pimoroni. They don't provide\nCircuitPython drivers, but at least for some of the HATs there are\nready to use drivers for the builtin driver-IC. In only a few cases I\nhad some real porting work to do. But once I found out how to\ntranslate CPython I2C/SPI-calls to CircuitPython, the porting was\nstraightforward.\u003c/p\u003e\n\n\u003cp\u003e\u003cimg src=\"https://github.com/bablokb/pcb-pico-pi-base/blob/main/examples/4letter_hat-led_shim.jpg?raw=true\" alt=\"\"\u003e\u003c/p\u003e\n\n\u003ch2\u003eProject-Repository\u003c/h2\u003e\n\n\u003cp\u003eYou can find the project repository here:\n\u003ca href=\"https://github.com/bablokb/pcb-pico-pi-base\"\u003ehttps://github.com/bablokb/pcb-pico-pi-base\u003c/a\u003e. The repo has KiCad design\nfiles as well as ready to use production files for my preferred\nPCB manufacturer.\u003c/p\u003e\n\n\u003cp\u003eAlso in the repo are CircuitPython libraries and example code for all the\nHATs I tested or ported.\u003c/p\u003e\n\n\u003ch2\u003eNext Steps\u003c/h2\u003e\n\n\u003cp\u003eWhat I might do in the future is to create a similar adapter PCB for\nthe Feather form-factor. While in my current designs the Pico sits\ninbetween the PCB and the HAT, with the Feather I would probably\nmake the Feather plug in from behind.\u003c/p\u003e\n\n\u003cp\u003eThe second thing I am working on is to support the new\nWaveshare ESP32-S3-Pico. This is an ESP32-S3 in the Pico form-factor\nwith identical physical dimensions and identical pin-layout. This\nbreakout is interesting since it gives me a device with far more\nmemory than the Pico provides. And I don't have to create new\nadapter boards. First results look promising.\u003c/p\u003e\n\n\u003ch2\u003eCircuitPython Board Definition Files\u003c/h2\u003e\n\n\u003cp\u003eSince I have two form-factors and two revisions each with their own\npin-mapping, looking up the mapping is cumbersome. So I also created\nmy own CircuitPython versions that do the mapping for me. So\n\u003ccode class=\"inline\"\u003eboard.GPIO18\u003c/code\u003e will always map to the correct pin on the Pico, regardless\nwhich PCB I use.\u003c/p\u003e\n\n\u003cp\u003eWith two form-factors, two revisions and now three devices\n(Pico, Pico-W, EPS32-S3-Pico) I have a total of 16 combinations, thus\npotentially 16 CircuitPython versions. A lot to maintain, but not all\ncombinations are actually in use (yet).\u003c/p\u003e\n","metadata":{"markdown":"Running Pi-HATs with a Raspberry Pi Pico\n========================================\n\nBeing a Pi-user since the very first Pi1, I own many different\nPi-HATs.  Some of them are in daily use, but many of them are sitting\nin the shelf.  So I wondered if I could give them a second chance in\ncombination with a Pico. And one of my major use-cases are e-ink\ndisplays for the Pi.  These e-inks don't really match with the Pi,\nsince they are optimized for low-power scenarios. But even the Pi-Zero\ndrains batteries too fast to be a suitable partner for these kind of\ndisplays.\n\nAlthough equipped with a full 2x20 pin socket, most HATs only use a\nfew pins like power, ground, I2C or SPI. So using a bunch of jumper\ncables should already be sufficient. Although that is true and fine\nfor initial tests, a good and solid connection is always the better\nalternative.\n\nSo I did some research and discovered a few adapter-boards on the\nmarket that might be suitable. But on closer inspection it turned out\nthat most of them missed one important point: just mapping some\narbitrary pins is not enough. So I decided to create my own adapter\nboards.\n\n\nHardware\n--------\n\nOne board uses the Pi form-factor, the second uses the Pi-Zero\nform-factor:\n\n![](https://github.com/bablokb/pcb-pico-pi-base/blob/main/pico-zero-base.jpg?raw=true)\n![](https://github.com/bablokb/pcb-pico-pi-base/blob/main/pico-pi-base.jpg?raw=true)\n\nThey fit into standard enclosures, but since the USB-connection is on\nthe side they need an additional cutout. Changing available 3D-models\nshould be a simple task. And the bigger adapter PCB has a footprint\nfor a standard JST-2 battery connector exactly where the USB-power\ncutout is. Anyhow, this was not the major challenge when designing\nthese boards.\n\nThe biggest challenge was the correct pin-mapping. The Pi has I2C,\nUART, two SPI and I2S. I did not take the last one into account so I\nended up with two revisions. I2S needs two consecutive pins. On the\nPi, that is GPIO18 and GPIO19, but they are not next to each other on\nthe pin-header.\n\nAnother contraint was space. I did not want to route traces below the\nWLAN-chip and antenna of the Pico-W. In the end I had to make a\ncompromise for the Pi-Zero adapter: the first revision maps both SPIs\nbut not I2S, the second revision maps SPI0 and I2S but not SPI1. Which\nis not a big deal since I haven't found a HAT yet that actually uses\nSPI1.\n\nI also don't map the ID-pins of the Pi. These are used to\nautomatically configure the correct driver on the Pi. On the Pico, you\ndon't run a generic OS but a specific program, so you have to take\ncare about correct drivers already before when you put them on the\nCIRCUITPY-drive.\n\nThe Pi-adapter has more space. I added a SD-card reader and I broke\nout a number of pins. One of the drawbacks of many HATs is that they\nblock the complete pin-header although they only use a few pins.\nBreaking out the pins is not strictly necessary since you can access\nall pins from the back anyhow.\n\n\nSoftware\n--------\n\nThe second part of the project was porting the HAT-drivers to the Pico.\nFor Adafruit HATs, that was fairly simple. Adafruit has CircuitPython\nsupport for almost everything they sell. And since Blinka brings\nCircuitPython to the Pi-SBCs, \"porting\" the drivers is a matter of\nusing the correct pins.\n\nOn example: the speaker-bonnet. The learning guide\n(\u0026lt;https://learn.adafruit.com/adafruit-speaker-bonnet-for-raspberry-pi\u0026gt;)\ntells you it is using the I2S pins GPIO18, GPIO19 and GPIO21 on the Pi.\nAfter looking up the mapping for the adapters, you just plug in those\npins into a small example program provided by a second guide:\n(\u0026lt;https://learn.adafruit.com/mp3-playback-rp2040/pico-i2s-mp3\u0026gt;)\nand off you go playing MP3 on the speaker-bonnet. This is actually much\nsimpler on the Pico compared to the Pi, because you don't have to go\nthrough all the steps to install the relevant drivers.\n\nFor other HATs, you will usually find CircuitPython example code for\nthe Pi using Blinka in the learning guide for the HAT. In this case,\nyou can take the code as is and only replace the pin-numbers.\n\nI also own a number of HATs from Pimoroni. They don't provide\nCircuitPython drivers, but at least for some of the HATs there are\nready to use drivers for the builtin driver-IC. In only a few cases I\nhad some real porting work to do. But once I found out how to\ntranslate CPython I2C/SPI-calls to CircuitPython, the porting was\nstraightforward.\n\n![](https://github.com/bablokb/pcb-pico-pi-base/blob/main/examples/4letter_hat-led_shim.jpg?raw=true)\n\n\nProject-Repository\n------------------\n\nYou can find the project repository here:\n\u0026lt;https://github.com/bablokb/pcb-pico-pi-base\u0026gt;. The repo has KiCad design\nfiles as well as ready to use production files for my preferred\nPCB manufacturer.\n\nAlso in the repo are CircuitPython libraries and example code for all the\nHATs I tested or ported.\n\n\nNext Steps\n----------\n\nWhat I might do in the future is to create a similar adapter PCB for\nthe Feather form-factor. While in my current designs the Pico sits\ninbetween the PCB and the HAT, with the Feather I would probably\nmake the Feather plug in from behind.\n\nThe second thing I am working on is to support the new\nWaveshare ESP32-S3-Pico. This is an ESP32-S3 in the Pico form-factor\nwith identical physical dimensions and identical pin-layout. This\nbreakout is interesting since it gives me a device with far more\nmemory than the Pico provides. And I don't have to create new\nadapter boards. First results look promising.\n\n\nCircuitPython Board Definition Files\n------------------------------------\n\nSince I have two form-factors and two revisions each with their own\npin-mapping, looking up the mapping is cumbersome. So I also created\nmy own CircuitPython versions that do the mapping for me. So\n`board.GPIO18` will always map to the correct pin on the Pico, regardless\nwhich PCB I use.\n\nWith two form-factors, two revisions and now three devices\n(Pico, Pico-W, EPS32-S3-Pico) I have a total of 16 combinations, thus\npotentially 16 CircuitPython versions. A lot to maintain, but not all\ncombinations are actually in use (yet).\n"}}]