Reading Serial data from a Raspberry Pi Pico

This post documents what I did to get serial data from my Pico to be stored/displayed on my PC (which could equally be a full Raspberry Pi)

The reason for this is that I have lots of cool components from my Maker Advent Calendar (sensors, screen, lights etc) and while they work nicely just on the Pico if I wanted them to save the data somewhere so I can display them on a web site you can’t do that in micropython – or so I thought.

I started by following the instructions to get this little display attached to my Pico together with a light sensor to display current light readings.

https://thepihut.com/blogs/raspberry-pi-tutorials/maker-advent-calendar-day-11-omg-oled

I adapted it slightly to show a horizontal line as well (using this tutorial).

Published
Categorised as News

Getting a servo to turn on RPi 400

Thing I have learned:

  1. You don’t need a separate battery pack to power this continuous servo FS90R. You can power it from the 5V pin on the RPi
  2. I need to remember how breadboards work! The valley down the middle is NOT connected underneath
  3. I’m still not sure how pulse width modulation works so can’t control the turning well yet

Pics to follow soon

Useful link: https://stackoverflow.com/questions/45150825/control-continuous-servo-motor-with-raspberry-pi-and-python

Code:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
p = GPIO.PWM(17, 50)
p.start(7.5)
try:
  while True:
    p.ChangeDutyCycle(7.5)
    time.sleep(1)
    p.ChangeDutyCycle(12.5)
    time.sleep(1)
    p.ChangeDutyCycle(2.5)
    time.sleep(1)

except KeyboardInterrupt:
  GPIO.cleanup()
Published
Categorised as News

How to embed links from Repl.it

Add a custom HTML block

Then use an iframe with the following format

<iframe frameborder="0" width="100%" height="500px" src="https://replit.com/@USERNAME/demo-embed?embed=true"></iframe>

Published
Categorised as News

How to embed links from Gists to an iframe

Our VLE doesn’t allow us to embed <script> tags which is the only way that Gist allows embeds of code but I found this alternative which seems to work.

TLDR; add .pibb to the end of the shareable link

<iframe src="https://gist.github.com/laura-james/f42fddb216f486eb80ac1b955f3618d2.pibb" style="width: 100%">
</iframe>

 

It works!

 

Just wish it was that easy to do from Replit – it just seems to want to show a cover image when I just want it to show the main.py file. Grrr!

Published
Categorised as News

Adventures with microbits

It’s the Raspberry Pi Competition with PA Consulting again and this year’s topic is about healthcare and well-being.

The students who want to do the competition have been talking about sensors to read heart rate, step count, crash detection. We are constrained by budget (there are great components specifically for the Pi we can use – like the senseHat) but we have lots of microbits at school – I wondered if we could use them to record something like step count and then send that data over radio?

Idea sketched out on envelope!

The Microbit 1 code was easy enough:

 

The icons aren’t strictly necessary but give the user a bit of feedback that the code is working. This would be running on the users microbit that would be powered by a battery pack.

Microbit 2 provided to be a trickier problem. I know Micropython can be run directly on the Microbit. However I came to the conclusion that it would be impossible/tricky to get data out of it and into the Pi. Yes there are posts on the web about people who do it via the serial interface, but I felt this would have been too tricky to explain to my Y7-11 students.

Then I discovered bitio (https://github.com/whaleygeek/bitio ) a library that allows you to connect to your attached microbit using Python, thus allowing you to run the full set of Python commands (who knew MicroPython has different implementations of how you access and write to a file?)

Steps:

  • Download the code from git hub and Unzip the file locally
  • Drag bitio.hex to your microbit – wait till it updates and you should see the bitio logo on it. (I discovered a problem with dragging things to my microbit from Windows pcs – and ended up having to use this cmd to ensure it does it properly :
robocopy /z . D:\ bitio.hex

  • Try running some of the example programs in the included src folder to connect your microbit first

I then updated the version of radio.py after much faffing about to this:

This is running on Thonny on my computer (haven’t even started thinking about putting it on my Pi yet!)

As you can see it is saving all button presses/shakes on the remote microbit to a text file – “received.txt”

The easiest way I could see of people seeing these inputs was to make a little Flask webpage on my computer (will be Raspberry PI) that just reads in this text file and puts it on the screen. The code is below and it’s running in Pycharm (so yes I have two Python programs running at once)

With the index.html here:

As you may have spotted the page refreshes every 2 secs to get any updated data – this could probably be done better, maybe using Ajax?

Next steps?

Make it more useful I suppose!

  • For example – use a basic step counter program on microbit 1 to record all your steps for a day.
  • Then on pressing of a button it is broadcast to microbit 2.
  • Microbit 2 could be listening out for each step count for the member of each person in the family for example
  • And then the website could display the stats for each, so you could have a race between each member of the family?
  • Not sure that having a local text file is the best way to do this, perhaps I should be posting it to a public website using the requests module.

Again not sure how much this is using a Raspberry pi as this is mostly leveraging the power of microbits! But it’s a start….

Published
Categorised as News

Conditional Formatting – when cell contains text

Its the start of a new year and I’m getting my class timetable from our school system. I like working with colour coding so I can see what years I’m teaching. I used to do it manually but today I looked into how to do it using conditional formatting in Excel. It’s a bit odd but it seems to work. You have to add it based on a formula and the formula I used is this:

=ISNUMBER(SEARCH($A$1,"9"))

..to look for a cell that contains “9”. Note: $A$1 is the first cell of the block I have selected. And I make the background colour change to yellow.

I’ve added 4 other rows depending on what Year groups I’m teaching (eg 10, 11, 12 and 13)

It should also make it easier to change the colours if I think they’re a bit naff!

Another useful conditional formatting rule I discovered to highlight those students who missed marks in each question is shown below:

See how the max mark is shown at the top of the column (row 8) and the student marks are from G12 across and down. It just makes it easy to spot patterns where students haven’t got full marks.

Published
Categorised as News

Python Code to Know

This is the copy of the Jupyter Notebook I was working on Google Co Lab to show the Year 10/11s for the basics of Python programming. But alas Google Colab is blocked in school. However there is a way of publishing the notebook as a GitHub Gist. And the link below should show the gist (which is linked to the Colab notebook. Confused? I know I am!)

Published
Categorised as News

Mail Merge Word – using VBA to split a Word doc and name it

It’s back to school time and as usual I’m extracting data from Socrative reports.
Socrative.com is great when it comes to auto marking multiple choice content but if I have a few manually marked questions then it becomes painful in both adding up the marks and giving the students feedback as to where they can improve.

Published
Categorised as News