YouTube2Text

Building a GPS Mechanical Clock: Getting Started With The Raspberry Pi Pico — Transcript

by Clough42 · 5,863 words · 845 segments · language en · Watch on YouTube

Full transcript

  1. 0:00We are in the electronics lab today
  2. 0:02because I am ramping up a new project
  3. 0:04that is going to use a Raspberry Pi Pico
  4. 0:06microcontroller.
  5. 0:08Now, I've never used this platform
  6. 0:09before, but I've spent the last 3 weeks
  7. 0:12prototyping and learning, and while I
  8. 0:15had a lot of fun, I'm not going to put
  9. 0:16you through all of that, but I will show
  10. 0:18you some of the things that I learned,
  11. 0:20and maybe I can save you some time on
  12. 0:22your projects.
  13. 0:24>> [music]
  14. 0:26>> Welcome back to Cloud 42. I'm James.
  15. 0:29Well, I'm getting a head start on a
  16. 0:31project that I'm planning to do later
  17. 0:33this year. I want to build a mechanical
  18. 0:35clock that's driven off of a GPS time
  19. 0:39source. Now, there's a lot to figure out
  20. 0:42on this, and I'm still working on the
  21. 0:44mechanics. I want to do something that's
  22. 0:46really ridiculous, and that you just
  23. 0:48can't help but smile when you see it.
  24. 0:50I'm not quite ready to get into that
  25. 0:52yet, but today I'm going to get a head
  26. 0:54start on the electronics and the
  27. 0:56software. We're going to use a Raspberry
  28. 0:59Pi Pico microcontroller. We're going to
  29. 1:01use a GPS receiver to get accurate time.
  30. 1:04Got some input buttons. I'm going to
  31. 1:06have a TFT display. I'm going to have
  32. 1:07six stepper motors with stepper drivers
  33. 1:10and some Hall effect sensors for homing.
  34. 1:13So, we're going to pretty much use all
  35. 1:15the pins on this microcontroller and a
  36. 1:17few of the peripherals, and uh
  37. 1:20I spent a lot of time figuring out how
  38. 1:21to make this thing work, and I'm going
  39. 1:23to try to save you some trouble by
  40. 1:25showing you what I have learned. These
  41. 1:27are the electronic parts that I'm
  42. 1:29planning on using for this project,
  43. 1:30starting first with the Raspberry Pi
  44. 1:32Pico. Now, if you're not familiar with
  45. 1:34the Pico, this is a microcontroller from
  46. 1:37the Raspberry Pi Foundation, and it's a
  47. 1:40pretty capable little controller, but
  48. 1:43the real claim to fame is that these
  49. 1:45things are cheap. They sell these This
  50. 1:48is the Raspberry Pi Pico one with the
  51. 1:50RP2040 chip. They sell these for $4.
  52. 1:54Now, they have versions with Wi-Fi and
  53. 1:56Bluetooth on them, and now there's a
  54. 1:58Raspberry Pi Pico 2 that's a little bit
  55. 2:01faster, has a floating point processor,
  56. 2:03and a version of that also with the
  57. 2:05Wi-Fi. But, I'm just using the Raspberry
  58. 2:07Pi Pico 1. This thing has plenty of
  59. 2:11horsepower for the project that I have
  60. 2:13in mind. It has enough IO pins, and I
  61. 2:16don't need the Wi-Fi at all. Now, you'll
  62. 2:18note that I have removed the pins from
  63. 2:21this and soldered in some extra long
  64. 2:23ones, so we have posts sticking up, and
  65. 2:26I will show you what that is for in a
  66. 2:28little bit.
  67. 2:29Now, as our time source, I've got a
  68. 2:31little cheap GPS module. Just picked
  69. 2:33this up off of Amazon. This is the
  70. 2:36GTU-7, and it's got a little antenna
  71. 2:40connector for a little patch antenna
  72. 2:41that it came with, and this can receive
  73. 2:43GPS signals. I don't care about
  74. 2:46location. I'm really only interested in
  75. 2:48getting a really accurate time off of
  76. 2:50that and feeding it into the
  77. 2:51microcontroller.
  78. 2:53And then, I'm also going to have a
  79. 2:54little tiny TFT display. This is not the
  80. 2:57main time display. This is just for
  81. 2:58configuration, stuff like time zones,
  82. 3:01and checking the GPS signal strength.
  83. 3:04And then, on the output side, we're
  84. 3:06going to use TMC2208
  85. 3:09stepper drivers. And these are the
  86. 3:12Silent Step Stick drivers. These are all
  87. 3:15over all of the usual sources from uh
  88. 3:18import sources, and they're really,
  89. 3:21really cheap. We're going to use those
  90. 3:22to drive these NEMA 14 stepper motors.
  91. 3:25Now, these were actually the genesis of
  92. 3:27the project because I have 40 of these
  93. 3:29sitting in a tub because reasons, left
  94. 3:32over from a previous project, and I
  95. 3:34wanted to do something fun with them.
  96. 3:36So, we're going to use six of these to
  97. 3:37drive six digits of a mechanical clock.
  98. 3:40More details on that to come.
  99. 3:42And then,
  100. 3:43because the stepper motors just spin,
  101. 3:46and they're going to be driving a
  102. 3:47mechanism that displays the time, we're
  103. 3:49going to need to home those. So, I'm
  104. 3:51going to use Hall effect sensors to home
  105. 3:54them. And these are just some cheap Hall
  106. 3:56effect sensors that I also picked up off
  107. 3:58of Amazon. We'll take a little bit
  108. 4:00closer look at these modules in a bit.
  109. 4:03To power the Raspberry Pi Pico, we
  110. 4:05actually have a number of power pins
  111. 4:08that we can use. And I'm going to be
  112. 4:10using the V sys as the input. If we go
  113. 4:13dig into the data sheet, they actually
  114. 4:15show the power domains and the power
  115. 4:17schematic. We've got the USB connector
  116. 4:20and the power coming in from that, which
  117. 4:22is 5 volts, is called V bus.
  118. 4:25That goes through a diode to V sys. And
  119. 4:29both of those pins are broken out. We
  120. 4:30have the V bus and the V sys here.
  121. 4:33Then V sys goes through an onboard
  122. 4:36regulator to produce the 3.3 volts and
  123. 4:39the other lower voltages that are used
  124. 4:42by the actual microcontroller.
  125. 4:44So, what we want to do is we want to
  126. 4:45feed our power in here to V sys so that
  127. 4:49it will be regulated down to the 3V3.
  128. 4:53Now, because this is tied to the USB
  129. 4:56input, it's possible if you plug into
  130. 4:58USB for 5 volts to come out of V sys.
  131. 5:02And there are a bunch of different ways
  132. 5:04that they show that you can deal with
  133. 5:05this with MOSFETs and they have some
  134. 5:08other circuitry if you're going to be
  135. 5:09using batteries and chargers, but for
  136. 5:12me, I'm just going to feed the voltage
  137. 5:14in through V sys with a diode and call
  138. 5:16it a day. So, that is exactly what I
  139. 5:19have set up over here. You can see
  140. 5:21there's a little diode right here. This
  141. 5:23is my 5-volt supply and that 5-volt
  142. 5:26supply is coming from a buck converter.
  143. 5:29And the reason for this is I'm going to
  144. 5:31be driving these stepper motors and
  145. 5:33these stepper motors I'm going to want
  146. 5:35to run at 24 volts. So, I'm planning on
  147. 5:38running the entire clock off of a
  148. 5:3924-volt supply, which is what I have
  149. 5:42coming into my breadboard here. That
  150. 5:44will power the stepper drivers and then
  151. 5:46I'm running that through a buck
  152. 5:47converter that outputs 5 volts. I do
  153. 5:50need 5 volts for some other things on
  154. 5:52the board, but the microcontroller is
  155. 5:54going to be primarily a 3.3, but I'll
  156. 5:56take that 5 volts, feed it through a
  157. 5:58diode into the Raspberry Pi Pico, and
  158. 6:01then use the onboard regulator to
  159. 6:04regulate down to 3.3 volts.
  160. 6:06Now, the Pico actually has a pin here,
  161. 6:0936, that exposes that 3.3 volts, and
  162. 6:12that's intended as an output. So, that
  163. 6:15you can use that to power other stuff on
  164. 6:17your board. So, that's exactly what I've
  165. 6:19done here. That 3.3 volt output is
  166. 6:22linked over to a bus bar here on the
  167. 6:25breadboard, and I'm going to use that to
  168. 6:27run the GPS and the display. So, before
  169. 6:31I hook up anything else, and more
  170. 6:33importantly, before I risk frying
  171. 6:35anything else, let's power this up and
  172. 6:37make sure that all the power's connected
  173. 6:39properly. So, I'll switch on the 24-volt
  174. 6:41supply,
  175. 6:43and you can see I am getting a blinking
  176. 6:45light from the Pico, and that's just
  177. 6:47because I have a blinky sketch on there
  178. 6:49right now.
  179. 6:50And if we check the 24-volt input, which
  180. 6:53is right here,
  181. 6:55is indeed 24 volts. The output of the
  182. 6:58buck converter is
  183. 7:005 volts.
  184. 7:02And then after that goes through the
  185. 7:03diode, we have a little bit of a drop.
  186. 7:05That's 4.2. That should be plenty. And
  187. 7:08then the output from the Raspberry Pi
  188. 7:10Pico is
  189. 7:123.3 volts. So, I've got 3.3 volts there.
  190. 7:16Got 5 volts over here because this is
  191. 7:18where I'm going to hook up some things
  192. 7:19that need 5 volts.
  193. 7:21And I've got 24 volts over here.
  194. 7:24By the way, little quality of life tip,
  195. 7:26these are some probes that I bought off
  196. 7:28of Amazon, and these are extra sharp
  197. 7:30with narrow points, and they very easily
  198. 7:33fit into breadboards, so that you can
  199. 7:35just probe directly on the breadboard uh
  200. 7:38pin locations and not have to actually
  201. 7:40have something clipped on that sticks
  202. 7:42into the breadboard cuz these can
  203. 7:45probe directly.
  204. 7:50The peripheral pin outs on the Raspberry
  205. 7:52Pi Pico are flexible, meaning you can
  206. 7:54take the individual peripherals and you
  207. 7:55can map them to different sets of pins.
  208. 7:58There's a crossbar on the chip that
  209. 8:01allows you to actually move them around.
  210. 8:03Now, you can see here in the data sheet
  211. 8:05that most of the pins are marked as GP0
  212. 8:08through GP28, and these are GPIO pins.
  213. 8:12So, you can just digital read and
  214. 8:13digital write these and use them as
  215. 8:15individual signals.
  216. 8:17But also around the outside here, you
  217. 8:18see we have the peripherals marked like
  218. 8:21I2C buses, SPI buses, and UARTs. So, I'm
  219. 8:25planning on using the UART for actually
  220. 8:28serial communication to and from the GPS
  221. 8:32receiver. And I'm going to use SPI to
  222. 8:35communicate with the little TFT display.
  223. 8:38And by default, UART0 is up here on pins
  224. 8:411 and 2, but if you look, UART0 is also
  225. 8:44marked down here on pins 16 and 17, and
  226. 8:47it's also down here on pins 21 and 22.
  227. 8:51And you can control through the
  228. 8:53software, through the programming, which
  229. 8:55pins you want to use for UART0.
  230. 8:58Likewise, I'm going to use SPI0 for the
  231. 9:01communication with the TFT display, and
  232. 9:03you can have SPI0 down here on pins 21
  233. 9:06through 25, or you can have it up here
  234. 9:09on uh GP2 through GP5, pins 4 through 7,
  235. 9:13and there's some other duplicated pins
  236. 9:15all over the board, and you get to
  237. 9:17decide where you want to map those out.
  238. 9:20To program for the Raspberry Pi Pico,
  239. 9:22you have lots of options. It does
  240. 9:24support MicroPython, and lots of people
  241. 9:26love that, but I'm doing my development
  242. 9:28in C++, specifically on the Arduino
  243. 9:32platform. And the reason for that is
  244. 9:34just so that the code will be portable.
  245. 9:36I actually started this project working
  246. 9:38on the Teensy 4.0 and then switched over
  247. 9:42to the Raspberry Pi Pico because I was
  248. 9:43just kind of curious about the platform,
  249. 9:45especially since they are so
  250. 9:47inexpensive.
  251. 9:48And I'm doing my development not in the
  252. 9:50Arduino IDE, but in Visual Studio Code
  253. 9:54so that I have all of the tools that I'm
  254. 9:56used to using in my day job with an
  255. 9:58actual
  256. 9:59uh real IDE. Now, I am aware and I'm
  257. 10:02sure you're already typing it down in
  258. 10:03the comments that the Arduino IDE has
  259. 10:05come a long way in recent years and it
  260. 10:07has, but it's still not the same thing.
  261. 10:10So, I'm doing my work using PlatformIO
  262. 10:14and there is a plugin for PlatformIO
  263. 10:17that goes directly into Visual Studio
  264. 10:19Code. And so then to set up your
  265. 10:21project, all you have to do is create a
  266. 10:24PlatformIO.ini
  267. 10:25file specifying exactly what you want.
  268. 10:28In this case, I'm using a fork of the
  269. 10:31Raspberry Pi uh platform. I've defined
  270. 10:34the board as Pico. I've defined the
  271. 10:36framework as Arduino and specifically
  272. 10:39I'm using the Earl Philhower Arduino
  273. 10:42core. And that's because that has
  274. 10:45extensions in it that allow me to do
  275. 10:46some Raspberry Pi Pico specific things
  276. 10:49like using the multi-core processor and
  277. 10:53redefining the locations of the
  278. 10:55peripheral pins like we just talked
  279. 10:57about.
  280. 10:58The other thing that goes into your uh
  281. 11:00PlatformIO.ini are the dependencies that
  282. 11:03you're using. So, these are the
  283. 11:04libraries. I'm going to use Excel
  284. 11:05Stepper. I'm going to use Tiny GPS and
  285. 11:08I'm going to use some of the Adafruit
  286. 11:09libraries for the TFT display.
  287. 11:13And then there's a little bit here about
  288. 11:14uh build flags for where I have my
  289. 11:16include files and how I want to program
  290. 11:18it. But all you have to do is set up
  291. 11:21this file, save it, and PlatformIO will
  292. 11:24automatically pull down everything you
  293. 11:26need for the build environment. It pulls
  294. 11:28down the Arduino core. It pulls down all
  295. 11:30the libraries and it sets them up so
  296. 11:32that then you can just hit the little
  297. 11:34checkbox and it will compile everything.
  298. 11:36You don't have to worry about getting
  299. 11:37the libraries and downloading them. You
  300. 11:39don't have to worry if you're working
  301. 11:40with anybody else about telling them
  302. 11:42which libraries they have to get or
  303. 11:44install.
  304. 11:45If they check it out and they've got
  305. 11:46Platform IO set up in their dev
  306. 11:49environment, it'll just automatically
  307. 11:50pull everything.
  308. 11:52Now, there is one thing that Platform IO
  309. 11:53can't install for you and that is the
  310. 11:55USB driver. If you're working on
  311. 11:57Windows, you do need a special USB
  312. 11:59driver for Platform IO to be able to
  313. 12:01automatically put the Raspberry Pi Pico
  314. 12:04into mass storage mode so that it can
  315. 12:05upload firmware to it. The way it
  316. 12:07normally works is you press and hold the
  317. 12:10push button on the device while you turn
  318. 12:12it on. It goes into mass storage mode
  319. 12:14and it shows up as a USB drive on your
  320. 12:17computer and you copy the firmware over.
  321. 12:19The first time you have to do that
  322. 12:20manually and then if you go look at the
  323. 12:22getting started guide from the Raspberry
  324. 12:24Pi Foundation, there's a note in here
  325. 12:27about a tool that you can download and
  326. 12:29run
  327. 12:30to automatically install the correct
  328. 12:31WinUSB driver after you put it into mass
  329. 12:34storage mode the first time and then
  330. 12:36after that the uh Platform IO
  331. 12:39uh can use Pico tool to actually flash
  332. 12:42it, actually put it into that mode so
  333. 12:44you don't have to keep pushing the
  334. 12:45button. So, if we come back over here to
  335. 12:48my code here in Visual Studio, I can
  336. 12:50just
  337. 12:51click the little upload button and it
  338. 12:54will automatically do a build, it will
  339. 12:56put the device into flash mode and it'll
  340. 12:58transfer the firmware.
  341. 13:00I should probably turn that on, huh?
  342. 13:04I have the GPS receiver here on the
  343. 13:06breadboard and I have the transmit and
  344. 13:07receive pins connected to pins 1 and 2
  345. 13:10on the Raspberry Pi Pico, which is uh
  346. 13:13the UART that I plan to use to read
  347. 13:16them. Now, this is going to output
  348. 13:17serial data at 9600 baud, 3.3 V logic
  349. 13:21levels, and the software is going to
  350. 13:23read that. But, I don't know about you,
  351. 13:26every time I do something like this, I
  352. 13:28find it really easy to spend hours and
  353. 13:31hours and hours trying to figure out why
  354. 13:32I'm not reading any data. Is the GPS
  355. 13:35receiver not sending it? Do I have the
  356. 13:37connections wrong? Is it the software? I
  357. 13:40don't really know. And so, I like to
  358. 13:42hook up a logic analyzer for this. And
  359. 13:45this is why I have the extra long pins
  360. 13:47on the microcontroller.
  361. 13:49The Saleae logic analyzer, this is a
  362. 13:52Logic Pro 16, comes with socket pins on
  363. 13:56the ends of the wires, so you can easily
  364. 13:58just stick these on pins. If you want to
  365. 14:00stick them in the breadboard, you know,
  366. 14:01you can use an extension wire, stick it
  367. 14:03in there, but I find that trying to put
  368. 14:06a lot of logic analyzer pins into the
  369. 14:08breadboard ends up being trouble because
  370. 14:11they're super easy to pull out. They're
  371. 14:13much more secure if you have the extra
  372. 14:15long pins on the top of the
  373. 14:17microcontroller.
  374. 14:18So, let me connect a ground here to the
  375. 14:20ground pin.
  376. 14:22And I will connect one logic pin here to
  377. 14:24the data line that should be going from
  378. 14:27the GPS receiver to the microcontroller,
  379. 14:30and then I'll just turn on the power.
  380. 14:36You can see we've got a light on the GPS
  381. 14:38receiver.
  382. 14:41And it has now started flashing, which
  383. 14:43means it should actually be getting
  384. 14:45data, and that data should be flowing to
  385. 14:47the Pico. Now, my software actually
  386. 14:50flashes the light on the Pico when it
  387. 14:51receives sentences from the GPS, so that
  388. 14:53I see that's actually happening, but
  389. 14:56let's go look at the code and let's talk
  390. 14:58about the debugging options here.
  391. 15:01Whenever I do any kind of embedded
  392. 15:04software development project, I always
  393. 15:05have a configuration.h file that defines
  394. 15:08all of my pins. So, in this case, I'm
  395. 15:10defining the GPS baud rate 9600, so I
  396. 15:13can change that in one place. And then
  397. 15:15here are the pin assignments. So, GPS
  398. 15:18transmit on pin zero, GPS receive on pin
  399. 15:21one. And then I'll use those to set up
  400. 15:24the peripheral. So, if we go down into
  401. 15:26main here, I am actually calling serial
  402. 15:281.setReceive and set transmit with those
  403. 15:32pin definitions to reconfigure the
  404. 15:34serial port for that. And then in my
  405. 15:37main loop
  406. 15:39I am calling over to my GPS processor
  407. 15:42object and telling it to process
  408. 15:43incoming data. And all that's doing is
  409. 15:46checking to see if there's a character
  410. 15:47available on the serial port, reading
  411. 15:49it, and then encoding that with the
  412. 15:52TinyGPS++
  413. 15:54library. If that returns true, it means
  414. 15:57that that was the last character of a
  415. 15:58sentence and we can now go process that
  416. 16:00data.
  417. 16:01So, if I compile this and program it
  418. 16:12and then connect to the serial monitor
  419. 16:15you can see I've got output here saying
  420. 16:16the GPS time is valid. I got 11
  421. 16:18satellites with a horizontal dilution of
  422. 16:21precision of 0.8.
  423. 16:24Now, if I didn't actually see that
  424. 16:25output, how do we debug it? Well, I can
  425. 16:28bring up my logic analyzer now
  426. 16:30start that and I can see
  427. 16:34these data packets arriving on that pin.
  428. 16:37And further, I can actually set up an
  429. 16:39analyzer that receives that serial and
  430. 16:42decodes it. And so, in logic, I can
  431. 16:45actually see here the text as it's
  432. 16:48arriving from the GPS and I can use this
  433. 16:51as a debugging mechanism. So, I can see
  434. 16:53what it's actually sending. I could
  435. 16:55actually take this text and go decode it
  436. 16:57if I wanted, but in general just seeing
  437. 16:59that it's flowing and seeing that it
  438. 17:01looks correct tells me that my code
  439. 17:04should be receiving it.
  440. 17:07Can also stop this and zoom in on these
  441. 17:08individual packets and we can find some
  442. 17:11place with a bunch of sequential one
  443. 17:13bits and look at this and you can see
  444. 17:15over there on the right, 9.605
  445. 17:18kHz. So, that is actually arriving at
  446. 17:229600 baud as expected.
  447. 17:26Now, moving right along, I've got the
  448. 17:27display hooked up to the SPI bus pins,
  449. 17:31and there's a chip select, and there's a
  450. 17:33data command line in addition to the SPI
  451. 17:36pins. And then, just like I did with the
  452. 17:39serial port, I went ahead and connected
  453. 17:42some more pins over to my logic analyzer
  454. 17:45so I can monitor the communication back
  455. 17:47and forth.
  456. 17:49And if I flip this on, you can see that
  457. 17:51it does initialize. It is showing data.
  458. 17:54I need to
  459. 17:55get you a close-up of this so you can
  460. 17:57actually see what it looks like, but we
  461. 17:59do have data actually flowing down here.
  462. 18:01Let's go look at the logic analyzer and
  463. 18:03see exactly what is happening.
  464. 18:07In the logic analyzer, I have configured
  465. 18:09more pins here. We've got the chip
  466. 18:10select, we've got the data command,
  467. 18:12we've got uh MOSI, MISO, and SCLK. And
  468. 18:16so, this is the actual clock, and you
  469. 18:17can see bursts of data going to the
  470. 18:21display. And of course, we know that's
  471. 18:22working because we can see the data on
  472. 18:24the display, but if it weren't, this
  473. 18:25would be the way to debug it. So, if I
  474. 18:28stop and zoom in on these, you can see,
  475. 18:31just for starters, that the frequency of
  476. 18:33this data is much, much, much higher
  477. 18:35than the 9600 baud of the uh serial port
  478. 18:39from the GPS. In fact, if we zoom way in
  479. 18:42on the clock and take a look at what
  480. 18:43this actually is,
  481. 18:45you can see that this is running around
  482. 18:4840 or 42 MHz. So, that is actually using
  483. 18:54the hardware SPI peripheral in the
  484. 18:58Raspberry Pi Pico. I struggled with this
  485. 19:00for a while before I realized I had to
  486. 19:03get the Earl Philhauer core so that I
  487. 19:05could actually change the mappings
  488. 19:07because the
  489. 19:09uh TFT library I was using from Adafruit
  490. 19:12was actually doing a software SPI and it
  491. 19:15just ran a lot slower. And so to output
  492. 19:18the kind of data I wanted to send to the
  493. 19:19display, I couldn't send it in these
  494. 19:22little bursts every second. It was
  495. 19:23taking up a significant amount of this
  496. 19:25time.
  497. 19:28So specifically, I've got my pins
  498. 19:30defined over here in configuration.h
  499. 19:33and then in my main CPP, I'm actually
  500. 19:35calling SPI set receive set transmit set
  501. 19:38clock and setting that with the MISO,
  502. 19:40MOSI, and SCLK pins. And that actually
  503. 19:44took care of it and as soon as I did
  504. 19:46that, it immediately started running at
  505. 19:48multi-megahertz speeds rather than the
  506. 19:51much much slower kilohertz speeds that
  507. 19:53it was running at before I did that
  508. 19:55because I just wasn't using the hardware
  509. 19:57peripheral.
  510. 19:58Now, when I hook up the motors and the
  511. 20:00stepper drivers and actually want to
  512. 20:02start driving these things, I need to
  513. 20:04home the motors and I'm going to do that
  514. 20:06using a Hall effect sensor. And I've got
  515. 20:10one of these little Hall effect sensor
  516. 20:11breakout boards here that I bought on
  517. 20:13Amazon for cheap and these are the ones
  518. 20:15that I'm planning on using. Not
  519. 20:16specifically these boards but
  520. 20:18specifically these sensors. And there is
  521. 20:21a little bit of a problem with these.
  522. 20:23Now, these sensors actually do have a
  523. 20:25built-in LED. So if I go ahead and just
  524. 20:27hook this up, they said in the listing
  525. 20:31that they will run on 3.3 volts.
  526. 20:34That's not really realistic. They really
  527. 20:36do require 5 volts to be stable or at
  528. 20:39least like 4 and 1/2. And so I've got
  529. 20:41this set up and this board has a little
  530. 20:42LED on it. So if I take the south pole
  531. 20:45of a magnet and get it close, the little
  532. 20:47light comes on to show that it is
  533. 20:50triggered.
  534. 20:51And this is what's going on on that
  535. 20:54board. We've got the Hall effect sensor
  536. 20:55over here and it has what's called an
  537. 20:57open collector output. So the magnet
  538. 20:59comes up, it's got 5 volts supply and
  539. 21:01ground, and the output just gets
  540. 21:04switched to ground when the magnetic
  541. 21:07field is detected, specifically when the
  542. 21:08south pole of a magnet is detected. And
  543. 21:11that goes to a microcontroller that you
  544. 21:14would normally set up with a pull-up
  545. 21:15resistor, so that it will be normally
  546. 21:17high, and when this gets triggered, that
  547. 21:20open collector output pulls it low. But
  548. 21:22this little board actually has an LED on
  549. 21:25it, and that LED is connected through a
  550. 21:27resistor to the 5-V supply. So that when
  551. 21:30this switches on and pulls this line
  552. 21:33low, it also switches on that LED. Now,
  553. 21:36the inputs on the Raspberry Pi Pico are
  554. 21:38not 5-V tolerant. They can only accept
  555. 21:413.3 V on the input, maybe just a tiny
  556. 21:44tiny little bit more if you want to go
  557. 21:46to the absolute maximum rating. So, what
  558. 21:49actually is this line floating to
  559. 21:52because of this pull-up. Now, there will
  560. 21:54be some drop across the diode across the
  561. 21:56LED, but when there's not very much
  562. 21:59current flowing, there won't be very
  563. 22:00much of a drop.
  564. 22:02So, if we actually measure this and look
  565. 22:04at the power that we're providing in,
  566. 22:07that's 5 V. If we look at the output,
  567. 22:10it's floating up to 3.6. That is
  568. 22:13really close to the maximum. It's a
  569. 22:16little bit over the maximum input on a
  570. 22:18Raspberry, or excuse me, on a Raspberry
  571. 22:19Pi Pico pin.
  572. 22:21That's not good. Will we get away with
  573. 22:23it? Probably, but in my final design, I
  574. 22:27definitely want to use just the bare
  575. 22:28chip and not have that LED in there
  576. 22:31pulled up to 5 V, so I don't run the
  577. 22:33risk of damaging the Raspberry Pi Pico
  578. 22:36over time.
  579. 22:38Okay, the last thing I need to hook up
  580. 22:39to the microcontroller and get working
  581. 22:41are the motors. There will be six
  582. 22:42motors, six drivers, and six little Hall
  583. 22:45effect sensors to sense the position of
  584. 22:47the motor. And I went ahead and 3D
  585. 22:50printed a little wheel here with some
  586. 22:53numbers on it and with a magnet in the
  587. 22:54bottom to sense the Hall sensor, so
  588. 22:57that's the south pole of the magnet out,
  589. 22:59so that the Hall effect sensor will be
  590. 23:00triggered by it as it rotates.
  591. 23:02I don't have all the details of the
  592. 23:04mechanical clock yet, but I know that
  593. 23:06the motor is going to return is going to
  594. 23:07turn one full revolution to go through
  595. 23:09all 10 digits. So, I've just got all 10
  596. 23:11digits on this wheel, and even if I
  597. 23:13decide to do something else later, I can
  598. 23:14adjust the ratios. I at least have the
  599. 23:16bulk of the software figured out. So,
  600. 23:18this will just go on the motor, and then
  601. 23:20the hall sensor will need to fit
  602. 23:22underneath it so that that magnet will
  603. 23:23trigger it when it comes around. But,
  604. 23:26I've done this before. This is not my
  605. 23:27first rodeo, and I know having six of
  606. 23:29these things floating around on the desk
  607. 23:30is just going to be a nightmare. So, I
  608. 23:32went ahead and designed and 3D printed a
  609. 23:36little desk fixture for them.
  610. 23:39So, this has got spaces for six motors,
  611. 23:41six hall sensors, and it's got six
  612. 23:43little windows for my six little wheels
  613. 23:46with numbers to show through it. So, the
  614. 23:48motor just mounts underneath.
  615. 23:50There's a little fork here that can hold
  616. 23:52the little hall sensor chip in exactly
  617. 23:54the right place, and a place to screw
  618. 23:56down the board so that I can put the
  619. 23:58connector on it.
  620. 23:59Wheel goes on it. Number shows through.
  621. 24:02And just like any good cooking show, I
  622. 24:05have one that is already fully
  623. 24:08assembled.
  624. 24:09Well,
  625. 24:10almost fully assembled. I've got one
  626. 24:12more wheel of it needs to go on there.
  627. 24:16And so, I've got all the hall sensors
  628. 24:17hooked up to the Raspberry Pi Pico, and
  629. 24:20I've got all the motors hooked up to
  630. 24:22drivers, and I've got another board
  631. 24:24here.
  632. 24:27And of course, I've got a bunch more
  633. 24:29leads hooked up to my logic analyzer so
  634. 24:32I can monitor everything that's going
  635. 24:33on.
  636. 24:35So, uh let me get this powered up and
  637. 24:37show you how it works in practice.
  638. 24:41And then we'll go over to the computer
  639. 24:42and take a look at some of the details
  640. 24:44about the multi-core implementation and
  641. 24:46stuff like that.
  642. 24:47So, in theory, when this powers up, it
  643. 24:49should home all the motors to zero, and
  644. 24:51then as soon as it gets GPS time, it
  645. 24:54should start showing the GPS time.
  646. 24:57Let's power it up.
  647. 25:00And let's try that again with the wires
  648. 25:01actually connected to the correct
  649. 25:05inputs.
  650. 25:08There's the home. Now we're just waiting
  651. 25:10for GPS.
  652. 25:14And there's GPS. It is 6:22 and 6
  653. 25:18seconds.
  654. 25:19That happens to be p.m., but I can
  655. 25:21switch this to 24-hour time.
  656. 25:26So 18:22 15 16 17 18. And I can push
  657. 25:31this through different time zones, and
  658. 25:33it'll automatically update.
  659. 25:39Now I've had this running here on the
  660. 25:41bench for a couple of weeks just to kind
  661. 25:44of test the robustness of it and make
  662. 25:46sure that everything was going to
  663. 25:47survive and that the thermal performance
  664. 25:49of the drivers was going to be okay. And
  665. 25:51I've really been enjoying it, and I
  666. 25:53think you're going to really enjoy it
  667. 25:54when you see what I have in mind for the
  668. 25:56full clock.
  669. 25:58The AccelStepper library that I'm using
  670. 26:00to run the motors is not interrupt
  671. 26:02driven. You actually have to call it
  672. 26:04periodically to give it an opportunity
  673. 26:06to generate pulses for the motor. So
  674. 26:09here in my main loop, I am checking the
  675. 26:10buttons, I'm processing my incoming GPS
  676. 26:13data, and then I'm actually making a
  677. 26:15call to run all of the motors. And if we
  678. 26:17drill down here
  679. 26:19to the individual motors, I go through
  680. 26:22each one of them in sequence, call
  681. 26:24through, and ultimately call the
  682. 26:25AccelStepper run method. And all this
  683. 26:28does is it just checks to see if it's
  684. 26:30time to generate a pulse, and if it is,
  685. 26:33it generates a pulse to the motor. And
  686. 26:35this has to be run in your main loop so
  687. 26:36that this call gets called frequently.
  688. 26:39And if it doesn't get called frequently,
  689. 26:40then the performance of the motors is
  690. 26:43going to suffer.
  691. 26:44So one of the things that I did to try
  692. 26:46to debug this is in my run motors
  693. 26:48method, I actually designated a debug
  694. 26:51pin, and I set it to high before I run
  695. 26:54all the motors, and then I set it to low
  696. 26:56when I'm done. So, we can go over to the
  697. 26:59logic analyzer, and here's that motor
  698. 27:02run debug pin, and we can see the
  699. 27:04pulsing of the motors, and this gives us
  700. 27:06a way to find out if they are running
  701. 27:08smoothly. And as you can see,
  702. 27:11we're not running smoothly. So, if we
  703. 27:14zoom in here and see when nothing else
  704. 27:16is happening, how fast is this going?
  705. 27:19It's about 142
  706. 27:20kHz. So, that is plenty fast. I don't
  707. 27:24think I'm ever going to run over about
  708. 27:2512 kHz, so I'm easily 10 to 1
  709. 27:28servicing this routine faster than the
  710. 27:31motors are going to generate pulses.
  711. 27:33If you look down here at the bottom,
  712. 27:35here's the seconds display, and you can
  713. 27:37see where it generates the pulses to the
  714. 27:39motor. It accelerates, and then slows
  715. 27:41down again as it moves it from one digit
  716. 27:44to the next, and you can see maximum on
  717. 27:47this it's running right now about 2.6
  718. 27:49kHz. So, we're we're running plenty fast
  719. 27:52to service that motor. But look what
  720. 27:54happens when we start talking to the TFT
  721. 27:56display. That takes a little bit of
  722. 27:58time, and in this this case it took 31
  723. 28:01ms, and during that time we're not
  724. 28:03servicing the motors at all.
  725. 28:06Now, it it turns out that probably
  726. 28:08doesn't matter for this very specific
  727. 28:11application because you can see we got
  728. 28:14data from the GPS, and then we came down
  729. 28:19here and decided to update the display,
  730. 28:21and then that's the point at which we
  731. 28:23then told the motors to move. So, it
  732. 28:25probably doesn't end up being a really
  733. 28:27serious thing in this application, but
  734. 28:29in another application that might be a
  735. 28:31big deal. So, what I'm going to do is
  736. 28:34I'm going to use the second core to run
  737. 28:36this motor update routine.
  738. 28:38So, if you're using the Earl Philhauer
  739. 28:41core, you have not only a loop, you also
  740. 28:44have a loop one. And so, I can put that
  741. 28:48code to run the motors over in loop one,
  742. 28:51and it will set it will run on the
  743. 28:53second processor.
  744. 28:55Now, there's some things you have to do.
  745. 28:57In this case, I want the homing and
  746. 28:59everything to happen first. So, I'm
  747. 29:01using a FIFO here. So, there's also a
  748. 29:05setup and a setup one. One of those runs
  749. 29:08on each core. And what I'm doing is I'm
  750. 29:10using this FIFO, and I'm calling pop on
  751. 29:13it, saying wait for a value and then
  752. 29:15pull it off of that first-in-first-out
  753. 29:17buffer.
  754. 29:18And of course, that will block until
  755. 29:19something arrives. And then, in core
  756. 29:22zero, I'm doing all of the setup. And
  757. 29:24once it's all done, I'm just pushing a
  758. 29:26dummy value. It happens to be one onto
  759. 29:28that, and that will cause this pop to
  760. 29:30return and release the second core. So,
  761. 29:33the second core won't start doing
  762. 29:34anything until this is done.
  763. 29:38Now, the other thing that we need to
  764. 29:39worry about is we don't want to be
  765. 29:42updating values in the Excel stepper
  766. 29:45library with one core while the other
  767. 29:47core is processing.
  768. 29:49So, ultimately, all the way down here at
  769. 29:51the bottom,
  770. 29:53when I actually update the digits after
  771. 29:56I've done all the calculations, I call
  772. 29:58this rp2040.idle_other_core.
  773. 30:01Now, this is core zero. It's just done
  774. 30:03all the calculations. It's about to make
  775. 30:05calls to the Excel stepper library and
  776. 30:08tell it
  777. 30:09to move, which is going to update values
  778. 30:11in it. So, I say idle other core. It
  779. 30:13will stop the other core that's running
  780. 30:15the motor loop long enough to do this
  781. 30:18and then restart it. Now, technically,
  782. 30:20there's still a possibility that you
  783. 30:22could have some contention or that
  784. 30:24something might not be atomically
  785. 30:26updated or that the micro or that the
  786. 30:28compiler might do some optimization that
  787. 30:31causes problems. In this case, we're
  788. 30:33dealing with individual numbers. I don't
  789. 30:36think it's a problem. I haven't observed
  790. 30:38a problem. It's technically not
  791. 30:40perfectly safe, but I think we're going
  792. 30:43to get away with it.
  793. 30:44So, if we program this code now, where
  794. 30:46we're calling the run motors in loop one
  795. 30:49over to the Pico,
  796. 31:02now we go back to the logic analyzer and
  797. 31:04look.
  798. 31:05You'll see that there are no gaps here
  799. 31:08in the motor run.
  800. 31:10And that's because that's being run by
  801. 31:12the second core. So, while
  802. 31:15core zero is up here sending all this
  803. 31:17data out to update the TFT display, the
  804. 31:21second core is still servicing the
  805. 31:22motor. And in this case, you can see
  806. 31:25it's going along here and actually
  807. 31:26generating those pulses. What we were
  808. 31:28seeing earlier where these pulses
  809. 31:30started later, it's because the motor
  810. 31:33run was not actually happening and that
  811. 31:35work was being delayed. Now, you can see
  812. 31:37in here that it's pulsing along and when
  813. 31:41it actually has to send a pulse to the
  814. 31:42motor down here for the second ones
  815. 31:45digit, it actually takes a little bit
  816. 31:47longer when it actually has to output a
  817. 31:49pulse.
  818. 31:50And here where it's outputting pulses
  819. 31:52for two motors,
  820. 31:54you can see that the gap there's a gap
  821. 31:56there where it's outputting the pulse
  822. 31:57for the seconds tens digit, and then
  823. 32:00another gap where it's calling to the
  824. 32:01seconds one digit and outputting a
  825. 32:03pulse. That's because it actually has
  826. 32:05some work to do and it's not just
  827. 32:06returning immediately.
  828. 32:08Well, that's all I'm going to cover
  829. 32:09today. If you really are interested in
  830. 32:11all of the gory details, the source code
  831. 32:13is out on GitHub. I will put a link to
  832. 32:15that down in the video description. And
  833. 32:18you can look forward to the mechanical
  834. 32:20build coming up on the channel. I've got
  835. 32:22some fun ideas, or at least I think
  836. 32:25they're fun and I think you will, too.
  837. 32:26So, I am actually really looking forward
  838. 32:29to that. If you enjoyed this video, give
  839. 32:31it a thumbs up. Feel free to subscribe
  840. 32:34to the channel and maybe think about
  841. 32:35supporting the channel over on Patreon.
  842. 32:38The patrons of this channel are the ones
  843. 32:40who make projects like this possible.
  844. 32:42Thank you for watching.
  845. 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.