Get the most out of your smart hubs with Pybricks
Posted by Huw,
The announcement last month that Mindstorms is being discontinued sent shockwaves through the LEGO robotics community, with many wondering what the future holds and whether their investment in the platform has been wasted.
Furthermore, LEGO's strategy concerning the wider Powered Up platform, of which Mindstorms is part, seems muddled, and the promised enhancements to improve compatibility and usefulness have yet to materialise.
The main limitation for many is that the relatively ubiquitous and affordable Boost, Powered Up and Technic hubs cannot be used autonomously: they need to be tethered to a smart device via Bluetooth to operate which limits their application considerably, and although the motors and sensors intended for use with each of the hubs share a connector, they are not supported universally by the different software platforms needed to program them -- of which there are currently five!
What's needed is a way to write code for every hub using a common development environment, which supports every sensor and motor, and which enables it to run as soon as the hub is switched on, something especially useful in an exhibition environment, for example.
In time LEGO's refocused Build & Code team may well deliver that, but there is a solution available already: Pybricks.
What is Pybricks?
Pybricks is an open source, community project that enables Python to be run on smart hubs including the three listed above, and the Spike Prime/Mindstorms and Spike Essential ones as well.
Pybricks Python scripts are executed directly on the hub and the development environment used to create them will run on just about every computer and mobile device: Windows, Mac, IOS, Linux, Chromebook and Android, in fact pretty much anything with a web browser.
Pybricks supports plugging any motor or sensor with a PoweredUp connector into any hub, so if for example you have a Boost colour sensor, or even some original WeDo sensors, and you want to use them with the Mindstorms hub, you can do so. In fact the only device not supported by Pybricks is the original WeDo hub, which does not support firmware updates.
The latest version of the Pybricks development environment can be found at https://beta.pybricks.com and while it's officially still in beta I have had no problems with it. In fact, it seems very robust and complete.
Unlike the SPIKE app, which is designed primarily for kids to use in an educational setting, the Pybricks development environment emphasises productivity and ease of use for seasoned programmers, although that's not to say that it's not suitable for everyone.
There is more of a learning curve than using the SPIKE app's Scratch-based word blocks given that Python is a text-based language, but if you've ever programmed in another high-level language such as Basic, Pascal, JavaScript or C you'll be able to pick it up in no time.
Furthermore, the documentation is exceptional. It's easy to access, comprehensive, and full of useful examples.
Code-completion provides prompts as you type, to remind you of object methods and so on, which further simplifies learning it.
Getting started
Before you can use Pybricks with your hub(s) you need to flash them with new firmware, which might seem daunting, but it's quick, easy, and the official LEGO firmware can be reinstalled if needed.
Once that's done, you connect to the hub via Bluetooth, write your code, then download it to the hub in seconds. When you're happy with it, you can disconnect Bluetooth and run your program autonomously simply by switching the hub on and pressing the power button.
Making the Technic hub useful at last
One of the reasons I wanted to experiment with Pybricks is that I have something like a dozen of the Technic hubs that I thought might be useful for GBC modules, but with the official PoweredUp app you are limited to hooking them up to a tablet and keeping it connected while the program runs on it, which is slow, inefficient and totally impractical if you have multiple hubs and want to run them all day.
Pybricks also makes it possible to use the 88010 Remote Control with this hub, opening the door to remote control of the Technic Powered Up models without the need for a phone or tablet, although that's not something I've tried yet.
Incidentally, it's interesting to note that to buy a Technic hub from LEGO will cost you £80/$90, but you can get them on BrickLink for less than £15/$15 which I think is a good measure of how useful the community currently finds them!
I have now successfully written Python code for many of my GBC modules and, when 4 or fewer ports are needed, can utilise these plentiful hubs rather than the far more expensive SPIKE/Mindstorms ones. Pybricks has given them a new lease life.
The remaining disadvantage with them is that they are battery powered, thus unsuitable for running GBCs all day at a show, so I solved that problem by removing the battery holder and fitting a power socket on one end of the case, so they can be powered by a 9v transformer.
In a future article I will describe how I did that, and also provide sample code for a simple GBC module.
Verdict
Pybricks is a powerful platform that delivers what LEGO has so far been unable or unwilling to: complete compatibility across the entire Powered Up ecosystem, and a means to download your program onto any hub and have it run as soon as it is switched on.
As a result it has made the system far more useful, particularly the cheaper hubs and motors found in Technic and other sets. They are freed from their Bluetooth tether at last!
I have only just scratched the surface of what's possible with Pybricks and its advantages over LEGO's solution, but hope this article will inspire you to investigate it for yourself, and if you have any questions I will try and answer them, although I am far from being an expert.
94 likes
36 comments on this article
"Incidentally, it's interesting to note that to buy a Technic hub from LEGO will cost you £80/$90, but you can get them on BrickLink for less than £15/$15 which I think is a good measure of how useful the community currently finds them!"
Ouch I didn't actually notice that until now. Now I have to check what other powered up parts are available for cheap.
Well done to PyBricks (and Huw) for producing & promoting this great project in the right-to-repair vein!
I was surprised that the Mindstorms stuff wasn't on sale this past weekend.
This is a perfect answer to all those who are always lamenting about imminet obselence of things connectin via bluetooth. The coding community is huge, the potential is limitless and if something has value it will be kept alive by indepent enthusiasts. I watch this in retrocomputing community which is very big around the world and almost every obscure computer aswel as those the most popular from their era are getting new software, hardware, updates etc. constantly. Internet has enebaled global exchange of even most obscure ideas and anything lego related is very far from obscurity. Great project, kudos to developers!
As an owner of the original Mindstorms sets I really appreciate this news. My college girlfriend- now wife of 20yrs- bought my first Mindstorms as a birthday gift and I've been hooked ever since. Now own every subsequent generation in multiples. Probably the most open ended "toy" on the planet; there is literally no boundaries beyond the parts one owns and the imagination. This PyBricks software should only be an exclamation point on that statement.
Python powers the world, and this solution is an elegant and a straightforward one to the problems listed. Kudos to the PyBricks developers and thanks Huw for bringing it to the attention of a wider audience.
Dispite having no experience with coding I have gotten great use out of PyBricks. I can handle copy-coding and maybe changing a variable or two.
It's amazing what they have managed to get the hubs to do, including things Lego have been promising for a while, like on-hub programs.
I got 42124 and 42140 for a song because the sellers grandson didn't like using the app to play with them. Fixed that as soon as I got home with PyBricks thanks to code from other people.
Really looking forward to the next article.
I noticed today that 75253 Droid Commander now apprears as "coming soon" in the Powered Up app. I wonder if Boost and Mindstorm will appear there eventually.
Also, you might want to correct the "byprick" link :)
@emassicotte said:
"I noticed today that 75253 Droid Commander now apprears as "coming soon" in the Powered Up app. I wonder if Boost and Mindstorm will appear there eventually. "
Boost will, but the upgarde path for Mindstorms is the Spike app, which lacks some functions of the Mindstorms equivalent but is largely the same in terms of its programming capability.
This project is simply awesome, I've been using it to automate my train display in IsraeLUG's public events for over a year now.
I constantly have to prove to visitors that I am not hiding a remote or a mobile device somewhere...
This also opens up a great topic to chat about with young adults (and just "regular" adults) who may or may not have been exposed to the Lego robotics platform through FLL.
Can't wait for the update that will allow us to have hub-to-hub communication, so many options!
Going to have to find myself a decent Python tutorial. The only language I've been proficient in is Cobol, and that's a whole different bag of spanners.
@Davian said:
"As an owner of the original Mindstorms sets I really appreciate this news. My college girlfriend- now wife of 20yrs- bought my first Mindstorms as a birthday gift and I've been hooked ever since. Now own every subsequent generation in multiples. Probably the most open ended "toy" on the planet; there is literally no boundaries beyond the parts one owns and the imagination. This PyBricks software should only be an exclamation point on that statement."
Great post. Great story!
This is an interesting option which sounds like it would be worth exploring. It also looks like the Raspberry Pi Foundation's Build HAT and its power supply can be a viable replacement for Lego's various hubs, at least for anyone who happened to get a Raspberry Pi SBC while they were still cheap and plentifully. I'm surprised I've heard nothing about the Build HAT from the community so far. Once I get it working, I may have to post something myself.
Now I feel robbed of $90 for my technic hub! lol
I bookmarked a github project called node-poweredup a few years ago with the intention to try it out, but I've just never gotten around to it. PyBricks seems much farther along.
While this is certainly great news, I just wish that there was something educational for kids with the boost sets. I got my son the Star Wars set hoping it would teach him coding. Instead it just instructs you to drag this here and that there and then watch it work. My son does zero critical thinking and has no clue what any of the code means, it’s just a cool robot and excuse to get more screen time.
Again, I’m glad to see this exists, but Lego really needs to up their game in the educational department.
@huw
"so I solved that problem by removing the battery holder and fitting a power socket on one end of the case, so they can be powered by a 9v transformer."
Please, please, please do not forget this promise. Also, is the solution the sort of thing where you can connect it when needed and disconnect when you want to return to being mobile?
@audaver said:
" @huw
"so I solved that problem by removing the battery holder and fitting a power socket on one end of the case, so they can be powered by a 9v transformer."
Please, please, please do not forget this promise. Also, is the solution the sort of thing where you can connect it when needed and disconnect when you want to return to being mobile?"
Unfortunately not. Once modified the battery holder can't be inserted, but given the hubs are so cheap, just get another one!
This is great news. I always wanted Mindstorms growing up but it was too expensive. Still is.
Is there a database that lists the different versions of the Technic Hub? I found one from 2019 and another from 2020 for example.
I noticed that visually they are the same, but their price differs.
If anyone knows where we can find all the different properties of the powered up elements, I would appreciate.
@Kokan said:
"This is great news. I always wanted Mindstorms growing up but it was too expensive. Still is.
Is there a database that lists the different versions of the Technic Hub? I found one from 2019 and another from 2020 for example. I noticed that visually they are the same, but their price differs."
The only difference is that the earlier ones have a clip-on cover over the batteries whereas the later ones have a screw-on cover.
"If anyone knows where we can find all the different properties of the powered up elements, I would appreciate."
The Pybricks documentation is a good place to start.
I teach a high school Python course. This is awesome!
@Huw (or anyone else) any suggestions on a starter set or parts that I could buy, so my students could build and program something? Like maybe a car or a small robot. I would probably need to buy 4x copies so that the students could work together in small groups.
Thanks for sharing @Huw and thanks to everyone for your kind comments!
@wellspring: The following makes a pretty neat robotics starter pack:
1x Technic Hub. There is a variant with a clip-on cover or a screw-on cover. The hub is the same. Clip is more convenient. https://brickset.com/sets/88012-1/Technic-Hub
2x motors: For example: https://brickset.com/sets/88017-1/Large-angular-motor Any other motor will work too. But this one is pretty strong and not as expensive.
1x color-and-distance sensor: https://brickset.com/sets/88007-1/Colour-Distance-Sensor
Throw in some wheels and a few Technic elements to put everything together. You can make a line-following robot, an obstacle-avoidance robot, or a self-balancing robot.
If you shop around, you could get one set for about $70--$80.
@CCC said:
"I guess it depends how you do it. I have an old PF battery box that I drilled a small hole in one of the covers. Into that I inserted a cable from a 9V power supply with the end cut off. The bare wires get wrapped around the relevant battery terminals. I can easily unwrap the wires and re-use as a battery powered box if needed.
Although at some stage I will probably make a neater job and insert a proper socket into the cover and hard wire the insides so it only works on a power adapter. As for these hubs, they are cheap enough to destroy!
"
Hmmm, I like this plan! My nephews chew through AAs when they play with my Technic sets so I'll modify a battery box (so many lying around anyway) with a drilled hole & soldered wires from a 9V PSU. Thanks for the ideas!
(Ah, though I'll need to fill where the cells were with some lumps of steel as the battery is important ballast in some cases...!)
Time to also dig out the ol' NXT set and see if it plays in the new PyBricks future.
@Huw said:
" @Kokan said:
"This is great news. I always wanted Mindstorms growing up but it was too expensive. Still is.
Is there a database that lists the different versions of the Technic Hub? I found one from 2019 and another from 2020 for example. I noticed that visually they are the same, but their price differs."
The only difference is that the earlier ones have a clip-on cover over the batteries whereas the later ones have a screw-on cover.
"If anyone knows where we can find all the different properties of the powered up elements, I would appreciate."
The Pybricks documentation is a good place to start.
"
Thanks!
The documentation is indeed great and gives far more detail than I had expected.
I'm at awe at all the tech I had no ideia they had placed on these boxes!
It looks cool, but it appears to be tied to its own online IDE. Is there a way to use it with a mainstream IDE e.g. as an extension in Visual Studio Code?
Can you only connect one remote control at a time to the hub though?
> Is there a way to use it with a mainstream IDE e.g. as an extension in Visual Studio Code?
While we encourage beginning users to try the web IDE, we also have a command line utility that you can integrate with your own editors such as Visual Studio Code.
Pybricks Code does work offline as well, so you can still use it at LEGO events with no Wi-Fi available.
@Pybricks said:
"> Is there a way to use it with a mainstream IDE e.g. as an extension in Visual Studio Code?
While we encourage beginning users to try the web IDE, we also have a command line utility that you can integrate with your own editors such as Visual Studio Code.
Pybricks Code does work offline as well, so you can still use it at LEGO events with no Wi-Fi available."
The web IDE and offline app are more than adequate and you can indeed use the latter offline at exhibitions, even on a phone, which I found useful for quick troubleshooting last weekend.
The only feature I'd like added is a means to specify the file that should be downloaded when you press run. It defaults to the one you're currently editing, which can be a nuisnace if you're working on a library.
This is cool! Python is a good common language.
I was exited by PyBricks at first, but now there is a better option. I can't find love for the Python language, imo the programming equivalent of building a house with hammer and nails. It works, but there is better mounting equipment out there like Torx and a screwdriver. Python needs all that crappy import commands to do even the most rudimentary stuff like basic math. Also it sucks at integer boolean/math. Finally the indentation as structure element: and the object-everything mess. hub.motor.left.power(60) and crap chaining like that, yet you can as well call it cat.mew.doaway.litter(50) if you choose other declarations.
Today my go to is Bricklife's Scratch mod. The great thing about that is that you have full scratch to build fantastic user interfaces, react to camera and microphone, even make computer games that connect to LEGO. Also Bricklife Scratch works in every browser, not just Chrome. You can make really nice controllers!
I also got myself a Raspberry Pi HAT, not to use it on the Pi, but to use it as a motor interface for Arduino and the like, so you can add real great sensors. You program the attached sensors and motors over simple textual serial commands. Nice thing is you can give complex instructions for PID control.
@Nivst said:
"This project is simply awesome, I've been using it to automate my train display in IsraeLUG's public events for over a year now.
I constantly have to prove to visitors that I am not hiding a remote or a mobile device somewhere...
This also opens up a great topic to chat about with young adults (and just "regular" adults) who may or may not have been exposed to the Lego robotics platform through FLL.
Can't wait for the update that will allow us to have hub-to-hub communication, so many options!"
Hub-to-hub connection is the way forward (imagine huge line of assembly) but is a little impractical for mobile robots and such (lots of weight to carry around - might work for wheeled platform but certainly not for walkers). But very good for small mobile platform talking to other small mobile platform (cooperation).
While this is a good news (i.e. Pybrick) it is a shame that a company like Lego cannot support its product better (hubs have been out for a few years now, the software took forever to come out initially and as of now the 'servicing' is very sketchy).
Personally, I think it is better to just dive in and learn to program with a real programming language than moving brick icons around a screen (I actually find that harder and less intuitive). In the beginning, you only need to make a motor turn then stop then turn the other way and then you build on that - you don't even have to build anything - just testing the controls and the programming to achieve it. IMO
I love PyBricks as it opens up so many degrees of freedom when creating a MOC. I use it in combination with a USB Power Box from pv productions. This works great for the LEGO technic hub or control plus sets. I got it here: https://pv-productions.com/product/usb-power-box/?attribute_power-connector=PU+Technic+Hub
I use this USB power solution from pv productions. You can power your LEGO Technic hub or control plus hub via USB phone charger. Works great for me and I don't damage the lego enclosure. Here you can find more info: https://pv-productions.com/product/usb-power-box/?attribute_power-connector=PU+Technic+Hub
Couldn't agree more. They should consider a donation button to support the project
pv-Productions has many Lego Powered Up electronics available. They advertise on there product pages that community input is welcome and custom products are no problem from them. Perhaps contact them: https://pv-productions.com/product-tag/powered-up/
I got this product from them a few months back: https://pv-productions.com/product/powered-up-to-power-functions-adapter/
pv-Productions has many Lego Powered Up electronics available. They advertise on there product pages that community input is welcome and custom products are no problem from them. Perhaps contact them: https://pv-productions.com/product-tag/powered-up/
I got this product from them a few months back: https://pv-productions.com/product/powered-up-to-power-functions-adapter/