# Raspberry Pico based motor stage

**URL:** <https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025>\
**Category:** Contributions\
**Created:** [13 September 2022 18:48 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025 "2022-09-13T18:48:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pepa](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@Pepa](https://openflexure.discourse.group/u/Pepa)\
**Post date:** [13 September 2022 18:48 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/1 "2022-09-13T18:48:18Z")

</div>

Hi everyone, I designed motor stage hat for Raspberry based on Raspberry Pico instead of an arduino. I also ported sngaboard firmware so it works on pico.  
It uses serial on GPIO header instead of an USB cable which makes everything smaller.  
It needs a litle configuration, but works fine.

[https://oshwlab.com/alte/openflexure-board\_copy](https://oshwlab.com/alte/openflexure-board_copy)  
I hope it will be usefull.

Thera are two things which needs to be done to say it’s compleated:

- I dont have LED grid so it’s not tested.
- LED is powered via PIN capable of PWM. It would be nice to have possibility to control brightness directly from app. To do this an extension is needed for the Openflexure app but unfortunatelly this beyond my programming skills so it will take some time. The other thing which needs to be done is to add a new function to the Pico firmware but that will be easy.

---

<div class="post-metadata">

**Author:** ![WilliamW](https://yyz2.discourse-cdn.com/free1/user_avatar/openflexure.discourse.group/williamw/32/106_2.png) [@WilliamW](https://openflexure.discourse.group/u/WilliamW)\
**Post date:** [14 September 2022 10:33 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/2 "2022-09-14T10:33:07Z")

</div>

That is great work @Pepa. I was not able to see the ported firmware in your link above, only the hardware design. Looking at the board design around the camera cable slot there is only ground plane on top and bottom layers. That means that it is possible to extend the slot to the edge of the board, which makes it a bit easier to fit and remove the board and cable.

---

<div class="post-metadata">

**Author:** ![Pepa](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@Pepa](https://openflexure.discourse.group/u/Pepa)\
**Post date:** [14 September 2022 11:36 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/3 "2022-09-14T11:36:10Z")

</div>

Open slot is a great idea, I have updated the PCB according to your suggestion. I will add u2f file for Pico today.  
I’am working on some changes in code:

- PWM for motor control to lower motor current when motors don’t move. This will keep motors cooler without fear of losing position
- LED PWM

Because of that source code is a little bit messy at this point. I will upload it when i’am done

---

<div class="post-metadata">

**Author:** ![Pepa](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@Pepa](https://openflexure.discourse.group/u/Pepa)\
**Post date:** [15 September 2022 19:43 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/4 "2022-09-15T19:43:29Z")

</div>

I have updated firmware for Pico so now LED PWM works. Somehow I figured out (with the help of existing extensions and documentation) how to make my own extension. So now I can control the LED directly from the Openflexure app. The only problem is that I don’t know why extension icon doesnt appear.  
The last thing to do is to reduce the current of the motors at idle.

---

<div class="post-metadata">

**Author:** ![Pepa](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@Pepa](https://openflexure.discourse.group/u/Pepa)\
**Post date:** [17 September 2022 14:44 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/5 "2022-09-17T14:44:44Z")

</div>

Everything done, all files updated. Motors idle current can be reduced, value can be set by serial port and saved in EEPROM. Initial value is about 50% of working current. In my case motors are much cooler.

---

<div class="post-metadata">

**Author:** ![filip.ayazi](https://avatars.discourse-cdn.com/v4/letter/f/45deac/32.png) [@filip.ayazi](https://openflexure.discourse.group/u/filip.ayazi)\
**Post date:** [20 September 2022 13:20 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/6 "2022-09-20T13:20:48Z")

</div>

Amazing work @Pepa! I recently made a new Sangaboard based on the RP2040 microcontroller with integrated illumination control. The new Sangaboard firmware supports the pico and has partial support implemented for LED control (on/off only, the plan is to PWM it at high frequency to avoid flickering), the source is at [Files · sangaboard-v5 · Filip Ayazi / sangaboard-firmware · GitLab](https://gitlab.com/filipayazi/sangaboard-firmware/-/tree/sangaboard-v5) (the master branch also works on the pico), just adjust boards.h with your wiring.

PWM motor current control sounds awesome! Is the source for your firmware available somewhere?

---

<div class="post-metadata">

**Author:** ![Pepa](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@Pepa](https://openflexure.discourse.group/u/Pepa)\
**Post date:** [20 September 2022 17:42 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/7 "2022-09-20T17:42:10Z")

</div>

I have uploaded arduino sketch. Be aware that my sketch works only with arduino core from earlephilhower. I didn’t uploaded it before because I still need to clean it and do some changes. For this moment I just wanted to chceck if everything works fine.  
In my case PWMs are set to 50kHz so there is no flickering.

---

<div class="post-metadata">

**Author:** ![siddons](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@siddons](https://openflexure.discourse.group/u/siddons)\
**Post date:** [22 September 2022 18:15 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/8 "2022-09-22T18:15:06Z")

</div>

I’ve tried to download files from the web site, but it isn’t possible! One gets into a circular loop with being told you have to login to download, logging in, then being told again that you have to log in. Are the files really available for download?

---

<div class="post-metadata">

**Author:** ![Pepa](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@Pepa](https://openflexure.discourse.group/u/Pepa)\
**Post date:** [23 September 2022 19:39 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/9 "2022-09-23T19:39:28Z")

</div>

Yes files are available for download. I tried downloading them and everything worked fine.

---

<div class="post-metadata">

**Author:** ![siddons](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@siddons](https://openflexure.discourse.group/u/siddons)\
**Post date:** [25 September 2022 15:32 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/10 "2022-09-25T15:32:30Z")

</div>

Well, I tried again, using both Firefox and Chrome, with the same result. Oh well.

---

<div class="post-metadata">

**Author:** ![WilliamW](https://yyz2.discourse-cdn.com/free1/user_avatar/openflexure.discourse.group/williamw/32/106_2.png) [@WilliamW](https://openflexure.discourse.group/u/WilliamW)\
**Post date:** [28 September 2022 08:35 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/11 "2022-09-28T08:35:03Z")

</div>

The site does seem to require login for downloading the files. If you were able to post the files somewhere without a login it would be of wider availability to the community, or is there a setting you can make in OSHWlab to not need login for the files? You should be able to post the files here, unless it hits a file size limit or put them into an `issue` or merge request in the [OpenFlexure / openflexure-microscope-extra](https://gitlab.com/openflexure/openflexure-microscope-extra) section of the project on Gitlab.

---

<div class="post-metadata">

**Author:** ![justacec](https://avatars.discourse-cdn.com/v4/letter/j/839c29/32.png) [@justacec](https://openflexure.discourse.group/u/justacec)\
**Post date:** [1 January 2024 23:21 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/12 "2024-01-01T23:21:47Z")

</div>

I am interested in using a spare Pi Pico I have and have printed out everything (master stand and electronic drawer along with the nano conversion board that has the Pico seat and then some custom mounts for a different stepper motor board layout) but am not sure what the status of this is. Is there a good description on how to connect everything when using the RPi Pico vice the nano in the non-sanga board variants?

---

<div class="post-metadata">

**Author:** ![WilliamW](https://yyz2.discourse-cdn.com/free1/user_avatar/openflexure.discourse.group/williamw/32/106_2.png) [@WilliamW](https://openflexure.discourse.group/u/WilliamW)\
**Post date:** [1 January 2024 23:42 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/13 "2024-01-01T23:42:52Z")

</div>

In the main project, the Sangaboard V0.5 is based on the same microcontroller as the Pi Pico, but there is not yet a documented way to set up an actual Pico as a motor controller for Openflexure. There are some design decisions in the Sangaboard V0.5 that unfortunately use pins that are not available on the Pico. There are enough Sangaboard V0.5 in circulation that the pin choices are unfortunately locked in. The main motor functions are all on available pins, but extended functions use pins that are reserved on the Pico. This means that there should be a modified firmware used on a Pico to give only the main functions, to avoid any unexpected behaviours.

None of this is yet implemented. The fact that the Pico is 3.3V logic means that it is in principle also possible to use the serial port on the Pi instead of the USB, which frees the USB on the Pico as a power input for the motors. Again this is not documented because there is not yet a suitable firmware version.

---

<div class="post-metadata">

**Author:** ![justacec](https://avatars.discourse-cdn.com/v4/letter/j/839c29/32.png) [@justacec](https://openflexure.discourse.group/u/justacec)\
**Post date:** [10 January 2024 14:27 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/14 "2024-01-10T14:27:34Z")

</div>

I was looking at the current version of the [Filip Ayazi / sangaboard-firmware · GitLab](https://gitlab.com/filipayazi/sangaboard-firmware) repository. I have built the Pico firmware by setting the following defines in the config.h file:

```auto
    #define MCU_PICO
    #define BOARD_CUSTOM_PICO

```

and have seen the default pinout config for the pico in the boards.h file.

```auto
#elif defined(BOARD_CUSTOM_PICO)
    #define BOARD_STRING "Custom Pico board"
    #define WIRING_MOTOR_X 2, 3, 4, 5
    #define WIRING_MOTOR_Y 6, 7, 8, 9
    #define WIRING_MOTOR_Z 10, 11, 12, 13
    #define ENDSTOPS_INVERT false
    #define ENDSTOPS_PULLUPS true
    #define WIRING_ENDSTOPS_MIN {A0,A1,A2}
    #define WIRING_ENDSTOPS_MAX {A3,A4,A5}
    #define ADDITIONAL_STEPPERS 1
    #define WIRING_ADDITIONAL_STEPPER 0, 1, 22, 23
    #define WIRING_STEPSTICK {13,14,15} //enable, step, dir todo: move to boards.h
    #define PRIMARY_SERIAL_PORT Serial
    #define SECONDARY_SERIAL_PORT Serial2
    #define PWM_NUM 0
    #define WIRING_PWM_LEDS {}
    #define SUPPORT_EEPROM
    #define STAGE_MAX_MOTORS 4

```

So, my question is, some of the pinouts are specified as a number and some as an identifier (eg. A0, A1, A2). Is PIN number the PIN number on the board or a GPIO number?

Secondly, I see that the PWM\_NUM is set to 0, so that does that mean that is disabled? (I know there was some discussion that the PI Pico does not export all of the same pins as the Sangaboard.)

Also, I see there are elements here about end stops, I have not seen a build which factors in safety devices such as end stops. Is this a special variant?

---

<div class="post-metadata">

**Author:** ![filip.ayazi](https://avatars.discourse-cdn.com/v4/letter/f/45deac/32.png) [@filip.ayazi](https://openflexure.discourse.group/u/filip.ayazi)\
**Post date:** [10 January 2024 14:41 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/15 "2024-01-10T14:41:17Z")

</div>

It’s the GPIO number, I think the A\* numbers are a leftover from when the config was copied from an arduino based board, and since the endstops module is normally disabled it didn’t matter.

PWM\_NUM is set to 0 because using it (at least with a more powerful LED) requires additional electronics (a mosfet and a resistor), there should be no issues with enabling it on any available pico PWM pin.

I added support for endstops built into the microscope feet many years ago, there is an endstop module and if it is enabled in `config.h` it will check endstop status while moving and add homing support, but this is only implemented on the lowest levels of the stack (firmware, pysangaboard). As far as I know no one has used this in years, so consider it experimental.

---

<div class="post-metadata">

**Author:** ![justacec](https://avatars.discourse-cdn.com/v4/letter/j/839c29/32.png) [@justacec](https://openflexure.discourse.group/u/justacec)\
**Post date:** [10 January 2024 15:52 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/16 "2024-01-10T15:52:42Z")

</div>

Great! Thanks for the info.

I have a follow-on question on the pins. AFAIK, Serial is GP0 and GP1 but there there is the definition for the WIRING\_ADDITIONAL\_STEPPER which is set to use GPIOs 0, 1, 22, and 23. This seems to step on the default Serial definition. I have just disabled that stepper by setting the STAGE\_MAX\_MOTORS to 3. Is this a correct approach.

As for powering the Pico, I plan to run a 5V off the RPi to the VBUS pin on the Pico and then run a 5V from a wall wart to power the motors and tie all of the grounds together.

The LED is running off the 5V line on the Raspberry Pi 4 as well.

Does all of this sound good to go for right now?

---

<div class="post-metadata">

**Author:** ![filip.ayazi](https://avatars.discourse-cdn.com/v4/letter/f/45deac/32.png) [@filip.ayazi](https://openflexure.discourse.group/u/filip.ayazi)\
**Post date:** [10 January 2024 21:55 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/17 "2024-01-10T21:55:20Z")

</div>

Sounds good to me, those are the default pins for Serial but they can be changed somewhat and there are 2 UARTs available if I remember correctly, the Sangaboard uses serial via pins 8 and 9 so pin 0 was available for the extra stepper (`WIRING_SERIAL_*` defines in `boards.h`). There’s certainly no harm in disabling the extra stepper if you are not using it.

Unless you have some space restrictions I think it might be easier to use a USB connection between the pico and the pi, it should work out of the box and it avoids the need for separate pico power. However, using a direct serial connection to the Pi’s serial should work too.

---

<div class="post-metadata">

**Author:** ![justacec](https://avatars.discourse-cdn.com/v4/letter/j/839c29/32.png) [@justacec](https://openflexure.discourse.group/u/justacec)\
**Post date:** [11 January 2024 04:31 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/18 "2024-01-11T04:31:18Z")

</div>

This is my current progress. Not my best, but I think it might work.

 ![tempImageZWRK9q](https://global.discourse-cdn.com/free1/uploads/openflexure/original/2X/b/ba549e3a669cbd2850b0b361c0584f8ef86b3ac1.jpeg)

All I need to do now is connect the 12 wires for the steppers and I should be making good progress.

---

<div class="post-metadata">

**Author:** ![justacec](https://avatars.discourse-cdn.com/v4/letter/j/839c29/32.png) [@justacec](https://openflexure.discourse.group/u/justacec)\
**Post date:** [11 January 2024 11:45 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/19 "2024-01-11T11:45:51Z")

</div>

Well, I am not having the best of luck here. I have enabled the serial port and through the raspberry pi configuration routines (disabled the serial console but enabled the serial hardware) and have wired Pico GPIO pins 0 and 1 to the Raspberry Pi V4 GPIO 14 and 15 (with Tx → Rx and vice versa). I have done the `ofm update` and `off upgrade`, as well as the `ofm activate` and `pip install sangaboard` commands as suggested in other threads. I have also switch to git server by doing a `ofm develop`. Nothing seems to work. I keep getting this in my log files:

```auto
Jan 11 11:35:46 microscope python[507]: INFO:root:Trying port /dev/ttyAMA0 with settings {'baudrate': 115200, 'bytesize': 8, 'parity': 'N', 'stopbits': 1}
Jan 11 11:35:46 microscope python[507]: INFO:root:Creating serial.Serial instance...
Jan 11 11:35:46 microscope python[507]: WARNING:root:The serial port didn't close cleanly: 'Sangaboard' object has no attribute '_ser'
Jan 11 11:35:46 microscope python[507]: INFO:root:Trying port /dev/ttyAMA0 with settings {'baudrate': 115200, 'bytesize': 8, 'parity': 'N', 'stopbits': 1}
Jan 11 11:35:46 microscope python[507]: INFO:root:Creating serial.Serial instance...
Jan 11 11:35:46 microscope python[507]: WARNING:root:The serial port didn't close cleanly: 'Sangaboard' object has no attribute '_ser'
Jan 11 11:35:46 microscope python[507]: INFO:root:Trying port /dev/ttyS0 with settings {'baudrate': 115200, 'bytesize': 8, 'parity': 'N', 'stopbits': 1}
Jan 11 11:35:46 microscope python[507]: INFO:root:Creating serial.Serial instance...
Jan 11 11:35:46 microscope python[507]: INFO:root:Created Serial<id=0xb3a83690, open=True>(port='/dev/ttyS0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
Jan 11 11:35:46 microscope python[507]: INFO:root:Testing communication to SangaBoard
Jan 11 11:35:46 microscope python[507]: INFO:root:Running firmware checks...
Jan 11 11:35:46 microscope python[507]: INFO:root:Trying port /dev/ttyS0 with settings {'baudrate': 115200, 'bytesize': 8, 'parity': 'N', 'stopbits': 1}
Jan 11 11:35:46 microscope python[507]: INFO:root:Creating serial.Serial instance...
Jan 11 11:35:46 microscope python[507]: INFO:root:Created Serial<id=0xb3c76f50, open=True>(port='/dev/ttyS0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
Jan 11 11:35:46 microscope python[507]: INFO:root:Testing communication to SangaBoard
Jan 11 11:35:46 microscope python[507]: INFO:root:Running firmware checks...
Jan 11 11:35:46 microscope python[507]: ERROR:root:We don't have a serial port to open, meaning you didn't specify a valid port. Are you sure the instrument is connected?

```

Edit: I should add that I get the following when trying to access the Pico from the Pi directly:

```auto
sudo miniterm /dev/ttyAMA0 115200
--- Miniterm on /dev/ttyAMA0 115200,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
B␀␀␕␀␀␑␀␀
␀ED␀
␀␀␀���␒␀␀␂␀␀␀␒␕"␔␀␑␀E␀␀␀���␒␀␑␀␀␀␀␀␀␀␀␀␀␀␀␀␕␀␀␀␀␀␀␀���␒␀␀␀RHH␀␀��␒␀␀␀HH␀␀␀␀␀	␀␑␀␀␀␀␀␀␀␀␀"	␑␀␀␀␀␀	␀	␀␑␀␂���␒␀␀␀RHH␀␔$␀␀␂���␒␀␀␀␑␀␀␀␀␀HH"␀␀␀␀R␀␀␂␀␀␀␀␀␀␀␀HHHH␀␀␀␀D␀␀␀␀␀␀␀␀␀RHH␀␀␀HH␀␀(�
)(␄�UE␅␀␀␀␀␀␀HH"��␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀�␀␀!␀␀U!D␀␀ ␀␀␀␀␀␀␔␀␀␀␀␀␀␀␀␀␀RHH␀␀␔␀␕␀␀␀␀␀␀␀�␀␂␀␀␀␑␀␀␒␑␀␀D␀␂␀␀␀␀␀␀␀␀␀␑␀␀␀���␒␀␑␒␀␀␀J
�
␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀␀
--- exit ---

```

---

<div class="post-metadata">

**Author:** ![filip.ayazi](https://avatars.discourse-cdn.com/v4/letter/f/45deac/32.png) [@filip.ayazi](https://openflexure.discourse.group/u/filip.ayazi)\
**Post date:** [11 January 2024 12:19 UTC](https://openflexure.discourse.group/t/raspberry-pico-based-motor-stage/1025/20 "2024-01-11T12:19:13Z")

</div>

Hmm, have you changed `SECONDARY_SERIAL_PORT` in `boards.h` to be `Serial1`?
