Building a GPS Mechanical Clock: Getting Started With The Raspberry Pi Pico — Transcript
Full transcript
- 0:00We are in the electronics lab today
- 0:02because I am ramping up a new project
- 0:04that is going to use a Raspberry Pi Pico
- 0:06microcontroller.
- 0:08Now, I've never used this platform
- 0:09before, but I've spent the last 3 weeks
- 0:12prototyping and learning, and while I
- 0:15had a lot of fun, I'm not going to put
- 0:16you through all of that, but I will show
- 0:18you some of the things that I learned,
- 0:20and maybe I can save you some time on
- 0:22your projects.
- 0:24>> [music]
- 0:26>> Welcome back to Cloud 42. I'm James.
- 0:29Well, I'm getting a head start on a
- 0:31project that I'm planning to do later
- 0:33this year. I want to build a mechanical
- 0:35clock that's driven off of a GPS time
- 0:39source. Now, there's a lot to figure out
- 0:42on this, and I'm still working on the
- 0:44mechanics. I want to do something that's
- 0:46really ridiculous, and that you just
- 0:48can't help but smile when you see it.
- 0:50I'm not quite ready to get into that
- 0:52yet, but today I'm going to get a head
- 0:54start on the electronics and the
- 0:56software. We're going to use a Raspberry
- 0:59Pi Pico microcontroller. We're going to
- 1:01use a GPS receiver to get accurate time.
- 1:04Got some input buttons. I'm going to
- 1:06have a TFT display. I'm going to have
- 1:07six stepper motors with stepper drivers
- 1:10and some Hall effect sensors for homing.
- 1:13So, we're going to pretty much use all
- 1:15the pins on this microcontroller and a
- 1:17few of the peripherals, and uh
- 1:20I spent a lot of time figuring out how
- 1:21to make this thing work, and I'm going
- 1:23to try to save you some trouble by
- 1:25showing you what I have learned. These
- 1:27are the electronic parts that I'm
- 1:29planning on using for this project,
- 1:30starting first with the Raspberry Pi
- 1:32Pico. Now, if you're not familiar with
- 1:34the Pico, this is a microcontroller from
- 1:37the Raspberry Pi Foundation, and it's a
- 1:40pretty capable little controller, but
- 1:43the real claim to fame is that these
- 1:45things are cheap. They sell these This
- 1:48is the Raspberry Pi Pico one with the
- 1:50RP2040 chip. They sell these for $4.
- 1:54Now, they have versions with Wi-Fi and
- 1:56Bluetooth on them, and now there's a
- 1:58Raspberry Pi Pico 2 that's a little bit
- 2:01faster, has a floating point processor,
- 2:03and a version of that also with the
- 2:05Wi-Fi. But, I'm just using the Raspberry
- 2:07Pi Pico 1. This thing has plenty of
- 2:11horsepower for the project that I have
- 2:13in mind. It has enough IO pins, and I
- 2:16don't need the Wi-Fi at all. Now, you'll
- 2:18note that I have removed the pins from
- 2:21this and soldered in some extra long
- 2:23ones, so we have posts sticking up, and
- 2:26I will show you what that is for in a
- 2:28little bit.
- 2:29Now, as our time source, I've got a
- 2:31little cheap GPS module. Just picked
- 2:33this up off of Amazon. This is the
- 2:36GTU-7, and it's got a little antenna
- 2:40connector for a little patch antenna
- 2:41that it came with, and this can receive
- 2:43GPS signals. I don't care about
- 2:46location. I'm really only interested in
- 2:48getting a really accurate time off of
- 2:50that and feeding it into the
- 2:51microcontroller.
- 2:53And then, I'm also going to have a
- 2:54little tiny TFT display. This is not the
- 2:57main time display. This is just for
- 2:58configuration, stuff like time zones,
- 3:01and checking the GPS signal strength.
- 3:04And then, on the output side, we're
- 3:06going to use TMC2208
- 3:09stepper drivers. And these are the
- 3:12Silent Step Stick drivers. These are all
- 3:15over all of the usual sources from uh
- 3:18import sources, and they're really,
- 3:21really cheap. We're going to use those
- 3:22to drive these NEMA 14 stepper motors.
- 3:25Now, these were actually the genesis of
- 3:27the project because I have 40 of these
- 3:29sitting in a tub because reasons, left
- 3:32over from a previous project, and I
- 3:34wanted to do something fun with them.
- 3:36So, we're going to use six of these to
- 3:37drive six digits of a mechanical clock.
- 3:40More details on that to come.
- 3:42And then,
- 3:43because the stepper motors just spin,
- 3:46and they're going to be driving a
- 3:47mechanism that displays the time, we're
- 3:49going to need to home those. So, I'm
- 3:51going to use Hall effect sensors to home
- 3:54them. And these are just some cheap Hall
- 3:56effect sensors that I also picked up off
- 3:58of Amazon. We'll take a little bit
- 4:00closer look at these modules in a bit.
- 4:03To power the Raspberry Pi Pico, we
- 4:05actually have a number of power pins
- 4:08that we can use. And I'm going to be
- 4:10using the V sys as the input. If we go
- 4:13dig into the data sheet, they actually
- 4:15show the power domains and the power
- 4:17schematic. We've got the USB connector
- 4:20and the power coming in from that, which
- 4:22is 5 volts, is called V bus.
- 4:25That goes through a diode to V sys. And
- 4:29both of those pins are broken out. We
- 4:30have the V bus and the V sys here.
- 4:33Then V sys goes through an onboard
- 4:36regulator to produce the 3.3 volts and
- 4:39the other lower voltages that are used
- 4:42by the actual microcontroller.
- 4:44So, what we want to do is we want to
- 4:45feed our power in here to V sys so that
- 4:49it will be regulated down to the 3V3.
- 4:53Now, because this is tied to the USB
- 4:56input, it's possible if you plug into
- 4:58USB for 5 volts to come out of V sys.
- 5:02And there are a bunch of different ways
- 5:04that they show that you can deal with
- 5:05this with MOSFETs and they have some
- 5:08other circuitry if you're going to be
- 5:09using batteries and chargers, but for
- 5:12me, I'm just going to feed the voltage
- 5:14in through V sys with a diode and call
- 5:16it a day. So, that is exactly what I
- 5:19have set up over here. You can see
- 5:21there's a little diode right here. This
- 5:23is my 5-volt supply and that 5-volt
- 5:26supply is coming from a buck converter.
- 5:29And the reason for this is I'm going to
- 5:31be driving these stepper motors and
- 5:33these stepper motors I'm going to want
- 5:35to run at 24 volts. So, I'm planning on
- 5:38running the entire clock off of a
- 5:3924-volt supply, which is what I have
- 5:42coming into my breadboard here. That
- 5:44will power the stepper drivers and then
- 5:46I'm running that through a buck
- 5:47converter that outputs 5 volts. I do
- 5:50need 5 volts for some other things on
- 5:52the board, but the microcontroller is
- 5:54going to be primarily a 3.3, but I'll
- 5:56take that 5 volts, feed it through a
- 5:58diode into the Raspberry Pi Pico, and
- 6:01then use the onboard regulator to
- 6:04regulate down to 3.3 volts.
- 6:06Now, the Pico actually has a pin here,
- 6:0936, that exposes that 3.3 volts, and
- 6:12that's intended as an output. So, that
- 6:15you can use that to power other stuff on
- 6:17your board. So, that's exactly what I've
- 6:19done here. That 3.3 volt output is
- 6:22linked over to a bus bar here on the
- 6:25breadboard, and I'm going to use that to
- 6:27run the GPS and the display. So, before
- 6:31I hook up anything else, and more
- 6:33importantly, before I risk frying
- 6:35anything else, let's power this up and
- 6:37make sure that all the power's connected
- 6:39properly. So, I'll switch on the 24-volt
- 6:41supply,
- 6:43and you can see I am getting a blinking
- 6:45light from the Pico, and that's just
- 6:47because I have a blinky sketch on there
- 6:49right now.
- 6:50And if we check the 24-volt input, which
- 6:53is right here,
- 6:55is indeed 24 volts. The output of the
- 6:58buck converter is
- 7:005 volts.
- 7:02And then after that goes through the
- 7:03diode, we have a little bit of a drop.
- 7:05That's 4.2. That should be plenty. And
- 7:08then the output from the Raspberry Pi
- 7:10Pico is
- 7:123.3 volts. So, I've got 3.3 volts there.
- 7:16Got 5 volts over here because this is
- 7:18where I'm going to hook up some things
- 7:19that need 5 volts.
- 7:21And I've got 24 volts over here.
- 7:24By the way, little quality of life tip,
- 7:26these are some probes that I bought off
- 7:28of Amazon, and these are extra sharp
- 7:30with narrow points, and they very easily
- 7:33fit into breadboards, so that you can
- 7:35just probe directly on the breadboard uh
- 7:38pin locations and not have to actually
- 7:40have something clipped on that sticks
- 7:42into the breadboard cuz these can
- 7:45probe directly.
- 7:50The peripheral pin outs on the Raspberry
- 7:52Pi Pico are flexible, meaning you can
- 7:54take the individual peripherals and you
- 7:55can map them to different sets of pins.
- 7:58There's a crossbar on the chip that
- 8:01allows you to actually move them around.
- 8:03Now, you can see here in the data sheet
- 8:05that most of the pins are marked as GP0
- 8:08through GP28, and these are GPIO pins.
- 8:12So, you can just digital read and
- 8:13digital write these and use them as
- 8:15individual signals.
- 8:17But also around the outside here, you
- 8:18see we have the peripherals marked like
- 8:21I2C buses, SPI buses, and UARTs. So, I'm
- 8:25planning on using the UART for actually
- 8:28serial communication to and from the GPS
- 8:32receiver. And I'm going to use SPI to
- 8:35communicate with the little TFT display.
- 8:38And by default, UART0 is up here on pins
- 8:411 and 2, but if you look, UART0 is also
- 8:44marked down here on pins 16 and 17, and
- 8:47it's also down here on pins 21 and 22.
- 8:51And you can control through the
- 8:53software, through the programming, which
- 8:55pins you want to use for UART0.
- 8:58Likewise, I'm going to use SPI0 for the
- 9:01communication with the TFT display, and
- 9:03you can have SPI0 down here on pins 21
- 9:06through 25, or you can have it up here
- 9:09on uh GP2 through GP5, pins 4 through 7,
- 9:13and there's some other duplicated pins
- 9:15all over the board, and you get to
- 9:17decide where you want to map those out.
- 9:20To program for the Raspberry Pi Pico,
- 9:22you have lots of options. It does
- 9:24support MicroPython, and lots of people
- 9:26love that, but I'm doing my development
- 9:28in C++, specifically on the Arduino
- 9:32platform. And the reason for that is
- 9:34just so that the code will be portable.
- 9:36I actually started this project working
- 9:38on the Teensy 4.0 and then switched over
- 9:42to the Raspberry Pi Pico because I was
- 9:43just kind of curious about the platform,
- 9:45especially since they are so
- 9:47inexpensive.
- 9:48And I'm doing my development not in the
- 9:50Arduino IDE, but in Visual Studio Code
- 9:54so that I have all of the tools that I'm
- 9:56used to using in my day job with an
- 9:58actual
- 9:59uh real IDE. Now, I am aware and I'm
- 10:02sure you're already typing it down in
- 10:03the comments that the Arduino IDE has
- 10:05come a long way in recent years and it
- 10:07has, but it's still not the same thing.
- 10:10So, I'm doing my work using PlatformIO
- 10:14and there is a plugin for PlatformIO
- 10:17that goes directly into Visual Studio
- 10:19Code. And so then to set up your
- 10:21project, all you have to do is create a
- 10:24PlatformIO.ini
- 10:25file specifying exactly what you want.
- 10:28In this case, I'm using a fork of the
- 10:31Raspberry Pi uh platform. I've defined
- 10:34the board as Pico. I've defined the
- 10:36framework as Arduino and specifically
- 10:39I'm using the Earl Philhower Arduino
- 10:42core. And that's because that has
- 10:45extensions in it that allow me to do
- 10:46some Raspberry Pi Pico specific things
- 10:49like using the multi-core processor and
- 10:53redefining the locations of the
- 10:55peripheral pins like we just talked
- 10:57about.
- 10:58The other thing that goes into your uh
- 11:00PlatformIO.ini are the dependencies that
- 11:03you're using. So, these are the
- 11:04libraries. I'm going to use Excel
- 11:05Stepper. I'm going to use Tiny GPS and
- 11:08I'm going to use some of the Adafruit
- 11:09libraries for the TFT display.
- 11:13And then there's a little bit here about
- 11:14uh build flags for where I have my
- 11:16include files and how I want to program
- 11:18it. But all you have to do is set up
- 11:21this file, save it, and PlatformIO will
- 11:24automatically pull down everything you
- 11:26need for the build environment. It pulls
- 11:28down the Arduino core. It pulls down all
- 11:30the libraries and it sets them up so
- 11:32that then you can just hit the little
- 11:34checkbox and it will compile everything.
- 11:36You don't have to worry about getting
- 11:37the libraries and downloading them. You
- 11:39don't have to worry if you're working
- 11:40with anybody else about telling them
- 11:42which libraries they have to get or
- 11:44install.
- 11:45If they check it out and they've got
- 11:46Platform IO set up in their dev
- 11:49environment, it'll just automatically
- 11:50pull everything.
- 11:52Now, there is one thing that Platform IO
- 11:53can't install for you and that is the
- 11:55USB driver. If you're working on
- 11:57Windows, you do need a special USB
- 11:59driver for Platform IO to be able to
- 12:01automatically put the Raspberry Pi Pico
- 12:04into mass storage mode so that it can
- 12:05upload firmware to it. The way it
- 12:07normally works is you press and hold the
- 12:10push button on the device while you turn
- 12:12it on. It goes into mass storage mode
- 12:14and it shows up as a USB drive on your
- 12:17computer and you copy the firmware over.
- 12:19The first time you have to do that
- 12:20manually and then if you go look at the
- 12:22getting started guide from the Raspberry
- 12:24Pi Foundation, there's a note in here
- 12:27about a tool that you can download and
- 12:29run
- 12:30to automatically install the correct
- 12:31WinUSB driver after you put it into mass
- 12:34storage mode the first time and then
- 12:36after that the uh Platform IO
- 12:39uh can use Pico tool to actually flash
- 12:42it, actually put it into that mode so
- 12:44you don't have to keep pushing the
- 12:45button. So, if we come back over here to
- 12:48my code here in Visual Studio, I can
- 12:50just
- 12:51click the little upload button and it
- 12:54will automatically do a build, it will
- 12:56put the device into flash mode and it'll
- 12:58transfer the firmware.
- 13:00I should probably turn that on, huh?
- 13:04I have the GPS receiver here on the
- 13:06breadboard and I have the transmit and
- 13:07receive pins connected to pins 1 and 2
- 13:10on the Raspberry Pi Pico, which is uh
- 13:13the UART that I plan to use to read
- 13:16them. Now, this is going to output
- 13:17serial data at 9600 baud, 3.3 V logic
- 13:21levels, and the software is going to
- 13:23read that. But, I don't know about you,
- 13:26every time I do something like this, I
- 13:28find it really easy to spend hours and
- 13:31hours and hours trying to figure out why
- 13:32I'm not reading any data. Is the GPS
- 13:35receiver not sending it? Do I have the
- 13:37connections wrong? Is it the software? I
- 13:40don't really know. And so, I like to
- 13:42hook up a logic analyzer for this. And
- 13:45this is why I have the extra long pins
- 13:47on the microcontroller.
- 13:49The Saleae logic analyzer, this is a
- 13:52Logic Pro 16, comes with socket pins on
- 13:56the ends of the wires, so you can easily
- 13:58just stick these on pins. If you want to
- 14:00stick them in the breadboard, you know,
- 14:01you can use an extension wire, stick it
- 14:03in there, but I find that trying to put
- 14:06a lot of logic analyzer pins into the
- 14:08breadboard ends up being trouble because
- 14:11they're super easy to pull out. They're
- 14:13much more secure if you have the extra
- 14:15long pins on the top of the
- 14:17microcontroller.
- 14:18So, let me connect a ground here to the
- 14:20ground pin.
- 14:22And I will connect one logic pin here to
- 14:24the data line that should be going from
- 14:27the GPS receiver to the microcontroller,
- 14:30and then I'll just turn on the power.
- 14:36You can see we've got a light on the GPS
- 14:38receiver.
- 14:41And it has now started flashing, which
- 14:43means it should actually be getting
- 14:45data, and that data should be flowing to
- 14:47the Pico. Now, my software actually
- 14:50flashes the light on the Pico when it
- 14:51receives sentences from the GPS, so that
- 14:53I see that's actually happening, but
- 14:56let's go look at the code and let's talk
- 14:58about the debugging options here.
- 15:01Whenever I do any kind of embedded
- 15:04software development project, I always
- 15:05have a configuration.h file that defines
- 15:08all of my pins. So, in this case, I'm
- 15:10defining the GPS baud rate 9600, so I
- 15:13can change that in one place. And then
- 15:15here are the pin assignments. So, GPS
- 15:18transmit on pin zero, GPS receive on pin
- 15:21one. And then I'll use those to set up
- 15:24the peripheral. So, if we go down into
- 15:26main here, I am actually calling serial
- 15:281.setReceive and set transmit with those
- 15:32pin definitions to reconfigure the
- 15:34serial port for that. And then in my
- 15:37main loop
- 15:39I am calling over to my GPS processor
- 15:42object and telling it to process
- 15:43incoming data. And all that's doing is
- 15:46checking to see if there's a character
- 15:47available on the serial port, reading
- 15:49it, and then encoding that with the
- 15:52TinyGPS++
- 15:54library. If that returns true, it means
- 15:57that that was the last character of a
- 15:58sentence and we can now go process that
- 16:00data.
- 16:01So, if I compile this and program it
- 16:12and then connect to the serial monitor
- 16:15you can see I've got output here saying
- 16:16the GPS time is valid. I got 11
- 16:18satellites with a horizontal dilution of
- 16:21precision of 0.8.
- 16:24Now, if I didn't actually see that
- 16:25output, how do we debug it? Well, I can
- 16:28bring up my logic analyzer now
- 16:30start that and I can see
- 16:34these data packets arriving on that pin.
- 16:37And further, I can actually set up an
- 16:39analyzer that receives that serial and
- 16:42decodes it. And so, in logic, I can
- 16:45actually see here the text as it's
- 16:48arriving from the GPS and I can use this
- 16:51as a debugging mechanism. So, I can see
- 16:53what it's actually sending. I could
- 16:55actually take this text and go decode it
- 16:57if I wanted, but in general just seeing
- 16:59that it's flowing and seeing that it
- 17:01looks correct tells me that my code
- 17:04should be receiving it.
- 17:07Can also stop this and zoom in on these
- 17:08individual packets and we can find some
- 17:11place with a bunch of sequential one
- 17:13bits and look at this and you can see
- 17:15over there on the right, 9.605
- 17:18kHz. So, that is actually arriving at
- 17:229600 baud as expected.
- 17:26Now, moving right along, I've got the
- 17:27display hooked up to the SPI bus pins,
- 17:31and there's a chip select, and there's a
- 17:33data command line in addition to the SPI
- 17:36pins. And then, just like I did with the
- 17:39serial port, I went ahead and connected
- 17:42some more pins over to my logic analyzer
- 17:45so I can monitor the communication back
- 17:47and forth.
- 17:49And if I flip this on, you can see that
- 17:51it does initialize. It is showing data.
- 17:54I need to
- 17:55get you a close-up of this so you can
- 17:57actually see what it looks like, but we
- 17:59do have data actually flowing down here.
- 18:01Let's go look at the logic analyzer and
- 18:03see exactly what is happening.
- 18:07In the logic analyzer, I have configured
- 18:09more pins here. We've got the chip
- 18:10select, we've got the data command,
- 18:12we've got uh MOSI, MISO, and SCLK. And
- 18:16so, this is the actual clock, and you
- 18:17can see bursts of data going to the
- 18:21display. And of course, we know that's
- 18:22working because we can see the data on
- 18:24the display, but if it weren't, this
- 18:25would be the way to debug it. So, if I
- 18:28stop and zoom in on these, you can see,
- 18:31just for starters, that the frequency of
- 18:33this data is much, much, much higher
- 18:35than the 9600 baud of the uh serial port
- 18:39from the GPS. In fact, if we zoom way in
- 18:42on the clock and take a look at what
- 18:43this actually is,
- 18:45you can see that this is running around
- 18:4840 or 42 MHz. So, that is actually using
- 18:54the hardware SPI peripheral in the
- 18:58Raspberry Pi Pico. I struggled with this
- 19:00for a while before I realized I had to
- 19:03get the Earl Philhauer core so that I
- 19:05could actually change the mappings
- 19:07because the
- 19:09uh TFT library I was using from Adafruit
- 19:12was actually doing a software SPI and it
- 19:15just ran a lot slower. And so to output
- 19:18the kind of data I wanted to send to the
- 19:19display, I couldn't send it in these
- 19:22little bursts every second. It was
- 19:23taking up a significant amount of this
- 19:25time.
- 19:28So specifically, I've got my pins
- 19:30defined over here in configuration.h
- 19:33and then in my main CPP, I'm actually
- 19:35calling SPI set receive set transmit set
- 19:38clock and setting that with the MISO,
- 19:40MOSI, and SCLK pins. And that actually
- 19:44took care of it and as soon as I did
- 19:46that, it immediately started running at
- 19:48multi-megahertz speeds rather than the
- 19:51much much slower kilohertz speeds that
- 19:53it was running at before I did that
- 19:55because I just wasn't using the hardware
- 19:57peripheral.
- 19:58Now, when I hook up the motors and the
- 20:00stepper drivers and actually want to
- 20:02start driving these things, I need to
- 20:04home the motors and I'm going to do that
- 20:06using a Hall effect sensor. And I've got
- 20:10one of these little Hall effect sensor
- 20:11breakout boards here that I bought on
- 20:13Amazon for cheap and these are the ones
- 20:15that I'm planning on using. Not
- 20:16specifically these boards but
- 20:18specifically these sensors. And there is
- 20:21a little bit of a problem with these.
- 20:23Now, these sensors actually do have a
- 20:25built-in LED. So if I go ahead and just
- 20:27hook this up, they said in the listing
- 20:31that they will run on 3.3 volts.
- 20:34That's not really realistic. They really
- 20:36do require 5 volts to be stable or at
- 20:39least like 4 and 1/2. And so I've got
- 20:41this set up and this board has a little
- 20:42LED on it. So if I take the south pole
- 20:45of a magnet and get it close, the little
- 20:47light comes on to show that it is
- 20:50triggered.
- 20:51And this is what's going on on that
- 20:54board. We've got the Hall effect sensor
- 20:55over here and it has what's called an
- 20:57open collector output. So the magnet
- 20:59comes up, it's got 5 volts supply and
- 21:01ground, and the output just gets
- 21:04switched to ground when the magnetic
- 21:07field is detected, specifically when the
- 21:08south pole of a magnet is detected. And
- 21:11that goes to a microcontroller that you
- 21:14would normally set up with a pull-up
- 21:15resistor, so that it will be normally
- 21:17high, and when this gets triggered, that
- 21:20open collector output pulls it low. But
- 21:22this little board actually has an LED on
- 21:25it, and that LED is connected through a
- 21:27resistor to the 5-V supply. So that when
- 21:30this switches on and pulls this line
- 21:33low, it also switches on that LED. Now,
- 21:36the inputs on the Raspberry Pi Pico are
- 21:38not 5-V tolerant. They can only accept
- 21:413.3 V on the input, maybe just a tiny
- 21:44tiny little bit more if you want to go
- 21:46to the absolute maximum rating. So, what
- 21:49actually is this line floating to
- 21:52because of this pull-up. Now, there will
- 21:54be some drop across the diode across the
- 21:56LED, but when there's not very much
- 21:59current flowing, there won't be very
- 22:00much of a drop.
- 22:02So, if we actually measure this and look
- 22:04at the power that we're providing in,
- 22:07that's 5 V. If we look at the output,
- 22:10it's floating up to 3.6. That is
- 22:13really close to the maximum. It's a
- 22:16little bit over the maximum input on a
- 22:18Raspberry, or excuse me, on a Raspberry
- 22:19Pi Pico pin.
- 22:21That's not good. Will we get away with
- 22:23it? Probably, but in my final design, I
- 22:27definitely want to use just the bare
- 22:28chip and not have that LED in there
- 22:31pulled up to 5 V, so I don't run the
- 22:33risk of damaging the Raspberry Pi Pico
- 22:36over time.
- 22:38Okay, the last thing I need to hook up
- 22:39to the microcontroller and get working
- 22:41are the motors. There will be six
- 22:42motors, six drivers, and six little Hall
- 22:45effect sensors to sense the position of
- 22:47the motor. And I went ahead and 3D
- 22:50printed a little wheel here with some
- 22:53numbers on it and with a magnet in the
- 22:54bottom to sense the Hall sensor, so
- 22:57that's the south pole of the magnet out,
- 22:59so that the Hall effect sensor will be
- 23:00triggered by it as it rotates.
- 23:02I don't have all the details of the
- 23:04mechanical clock yet, but I know that
- 23:06the motor is going to return is going to
- 23:07turn one full revolution to go through
- 23:09all 10 digits. So, I've just got all 10
- 23:11digits on this wheel, and even if I
- 23:13decide to do something else later, I can
- 23:14adjust the ratios. I at least have the
- 23:16bulk of the software figured out. So,
- 23:18this will just go on the motor, and then
- 23:20the hall sensor will need to fit
- 23:22underneath it so that that magnet will
- 23:23trigger it when it comes around. But,
- 23:26I've done this before. This is not my
- 23:27first rodeo, and I know having six of
- 23:29these things floating around on the desk
- 23:30is just going to be a nightmare. So, I
- 23:32went ahead and designed and 3D printed a
- 23:36little desk fixture for them.
- 23:39So, this has got spaces for six motors,
- 23:41six hall sensors, and it's got six
- 23:43little windows for my six little wheels
- 23:46with numbers to show through it. So, the
- 23:48motor just mounts underneath.
- 23:50There's a little fork here that can hold
- 23:52the little hall sensor chip in exactly
- 23:54the right place, and a place to screw
- 23:56down the board so that I can put the
- 23:58connector on it.
- 23:59Wheel goes on it. Number shows through.
- 24:02And just like any good cooking show, I
- 24:05have one that is already fully
- 24:08assembled.
- 24:09Well,
- 24:10almost fully assembled. I've got one
- 24:12more wheel of it needs to go on there.
- 24:16And so, I've got all the hall sensors
- 24:17hooked up to the Raspberry Pi Pico, and
- 24:20I've got all the motors hooked up to
- 24:22drivers, and I've got another board
- 24:24here.
- 24:27And of course, I've got a bunch more
- 24:29leads hooked up to my logic analyzer so
- 24:32I can monitor everything that's going
- 24:33on.
- 24:35So, uh let me get this powered up and
- 24:37show you how it works in practice.
- 24:41And then we'll go over to the computer
- 24:42and take a look at some of the details
- 24:44about the multi-core implementation and
- 24:46stuff like that.
- 24:47So, in theory, when this powers up, it
- 24:49should home all the motors to zero, and
- 24:51then as soon as it gets GPS time, it
- 24:54should start showing the GPS time.
- 24:57Let's power it up.
- 25:00And let's try that again with the wires
- 25:01actually connected to the correct
- 25:05inputs.
- 25:08There's the home. Now we're just waiting
- 25:10for GPS.
- 25:14And there's GPS. It is 6:22 and 6
- 25:18seconds.
- 25:19That happens to be p.m., but I can
- 25:21switch this to 24-hour time.
- 25:26So 18:22 15 16 17 18. And I can push
- 25:31this through different time zones, and
- 25:33it'll automatically update.
- 25:39Now I've had this running here on the
- 25:41bench for a couple of weeks just to kind
- 25:44of test the robustness of it and make
- 25:46sure that everything was going to
- 25:47survive and that the thermal performance
- 25:49of the drivers was going to be okay. And
- 25:51I've really been enjoying it, and I
- 25:53think you're going to really enjoy it
- 25:54when you see what I have in mind for the
- 25:56full clock.
- 25:58The AccelStepper library that I'm using
- 26:00to run the motors is not interrupt
- 26:02driven. You actually have to call it
- 26:04periodically to give it an opportunity
- 26:06to generate pulses for the motor. So
- 26:09here in my main loop, I am checking the
- 26:10buttons, I'm processing my incoming GPS
- 26:13data, and then I'm actually making a
- 26:15call to run all of the motors. And if we
- 26:17drill down here
- 26:19to the individual motors, I go through
- 26:22each one of them in sequence, call
- 26:24through, and ultimately call the
- 26:25AccelStepper run method. And all this
- 26:28does is it just checks to see if it's
- 26:30time to generate a pulse, and if it is,
- 26:33it generates a pulse to the motor. And
- 26:35this has to be run in your main loop so
- 26:36that this call gets called frequently.
- 26:39And if it doesn't get called frequently,
- 26:40then the performance of the motors is
- 26:43going to suffer.
- 26:44So one of the things that I did to try
- 26:46to debug this is in my run motors
- 26:48method, I actually designated a debug
- 26:51pin, and I set it to high before I run
- 26:54all the motors, and then I set it to low
- 26:56when I'm done. So, we can go over to the
- 26:59logic analyzer, and here's that motor
- 27:02run debug pin, and we can see the
- 27:04pulsing of the motors, and this gives us
- 27:06a way to find out if they are running
- 27:08smoothly. And as you can see,
- 27:11we're not running smoothly. So, if we
- 27:14zoom in here and see when nothing else
- 27:16is happening, how fast is this going?
- 27:19It's about 142
- 27:20kHz. So, that is plenty fast. I don't
- 27:24think I'm ever going to run over about
- 27:2512 kHz, so I'm easily 10 to 1
- 27:28servicing this routine faster than the
- 27:31motors are going to generate pulses.
- 27:33If you look down here at the bottom,
- 27:35here's the seconds display, and you can
- 27:37see where it generates the pulses to the
- 27:39motor. It accelerates, and then slows
- 27:41down again as it moves it from one digit
- 27:44to the next, and you can see maximum on
- 27:47this it's running right now about 2.6
- 27:49kHz. So, we're we're running plenty fast
- 27:52to service that motor. But look what
- 27:54happens when we start talking to the TFT
- 27:56display. That takes a little bit of
- 27:58time, and in this this case it took 31
- 28:01ms, and during that time we're not
- 28:03servicing the motors at all.
- 28:06Now, it it turns out that probably
- 28:08doesn't matter for this very specific
- 28:11application because you can see we got
- 28:14data from the GPS, and then we came down
- 28:19here and decided to update the display,
- 28:21and then that's the point at which we
- 28:23then told the motors to move. So, it
- 28:25probably doesn't end up being a really
- 28:27serious thing in this application, but
- 28:29in another application that might be a
- 28:31big deal. So, what I'm going to do is
- 28:34I'm going to use the second core to run
- 28:36this motor update routine.
- 28:38So, if you're using the Earl Philhauer
- 28:41core, you have not only a loop, you also
- 28:44have a loop one. And so, I can put that
- 28:48code to run the motors over in loop one,
- 28:51and it will set it will run on the
- 28:53second processor.
- 28:55Now, there's some things you have to do.
- 28:57In this case, I want the homing and
- 28:59everything to happen first. So, I'm
- 29:01using a FIFO here. So, there's also a
- 29:05setup and a setup one. One of those runs
- 29:08on each core. And what I'm doing is I'm
- 29:10using this FIFO, and I'm calling pop on
- 29:13it, saying wait for a value and then
- 29:15pull it off of that first-in-first-out
- 29:17buffer.
- 29:18And of course, that will block until
- 29:19something arrives. And then, in core
- 29:22zero, I'm doing all of the setup. And
- 29:24once it's all done, I'm just pushing a
- 29:26dummy value. It happens to be one onto
- 29:28that, and that will cause this pop to
- 29:30return and release the second core. So,
- 29:33the second core won't start doing
- 29:34anything until this is done.
- 29:38Now, the other thing that we need to
- 29:39worry about is we don't want to be
- 29:42updating values in the Excel stepper
- 29:45library with one core while the other
- 29:47core is processing.
- 29:49So, ultimately, all the way down here at
- 29:51the bottom,
- 29:53when I actually update the digits after
- 29:56I've done all the calculations, I call
- 29:58this rp2040.idle_other_core.
- 30:01Now, this is core zero. It's just done
- 30:03all the calculations. It's about to make
- 30:05calls to the Excel stepper library and
- 30:08tell it
- 30:09to move, which is going to update values
- 30:11in it. So, I say idle other core. It
- 30:13will stop the other core that's running
- 30:15the motor loop long enough to do this
- 30:18and then restart it. Now, technically,
- 30:20there's still a possibility that you
- 30:22could have some contention or that
- 30:24something might not be atomically
- 30:26updated or that the micro or that the
- 30:28compiler might do some optimization that
- 30:31causes problems. In this case, we're
- 30:33dealing with individual numbers. I don't
- 30:36think it's a problem. I haven't observed
- 30:38a problem. It's technically not
- 30:40perfectly safe, but I think we're going
- 30:43to get away with it.
- 30:44So, if we program this code now, where
- 30:46we're calling the run motors in loop one
- 30:49over to the Pico,
- 31:02now we go back to the logic analyzer and
- 31:04look.
- 31:05You'll see that there are no gaps here
- 31:08in the motor run.
- 31:10And that's because that's being run by
- 31:12the second core. So, while
- 31:15core zero is up here sending all this
- 31:17data out to update the TFT display, the
- 31:21second core is still servicing the
- 31:22motor. And in this case, you can see
- 31:25it's going along here and actually
- 31:26generating those pulses. What we were
- 31:28seeing earlier where these pulses
- 31:30started later, it's because the motor
- 31:33run was not actually happening and that
- 31:35work was being delayed. Now, you can see
- 31:37in here that it's pulsing along and when
- 31:41it actually has to send a pulse to the
- 31:42motor down here for the second ones
- 31:45digit, it actually takes a little bit
- 31:47longer when it actually has to output a
- 31:49pulse.
- 31:50And here where it's outputting pulses
- 31:52for two motors,
- 31:54you can see that the gap there's a gap
- 31:56there where it's outputting the pulse
- 31:57for the seconds tens digit, and then
- 32:00another gap where it's calling to the
- 32:01seconds one digit and outputting a
- 32:03pulse. That's because it actually has
- 32:05some work to do and it's not just
- 32:06returning immediately.
- 32:08Well, that's all I'm going to cover
- 32:09today. If you really are interested in
- 32:11all of the gory details, the source code
- 32:13is out on GitHub. I will put a link to
- 32:15that down in the video description. And
- 32:18you can look forward to the mechanical
- 32:20build coming up on the channel. I've got
- 32:22some fun ideas, or at least I think
- 32:25they're fun and I think you will, too.
- 32:26So, I am actually really looking forward
- 32:29to that. If you enjoyed this video, give
- 32:31it a thumbs up. Feel free to subscribe
- 32:34to the channel and maybe think about
- 32:35supporting the channel over on Patreon.
- 32:38The patrons of this channel are the ones
- 32:40who make projects like this possible.
- 32:42Thank you for watching.
- 32:45>> [music]
About this transcript
This page contains the full transcript of Building a GPS Mechanical Clock: Getting Started With The Raspberry Pi Pico by Clough42, generated from the public captions YouTube serves with the video. The transcript has 5,863 words across 845 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.