This project provides a Rust implementation for controlling a 28BYJ-48 stepper motor with a ULN2003 driver board using a Raspberry Pi Pico. The motor speed can be set in revolutions per minute (RPM).
- Raspberry Pi Pico
- 28BYJ-48 Stepper Motor
- ULN2003 Driver Board
- 5V power supply for the motor (can be from Pico's VBUS)
Connect the ULN2003 driver board to the Raspberry Pi Pico as follows:
ULN2003 -> Pico
IN1 -> GPIO2
IN2 -> GPIO3
IN3 -> GPIO4
IN4 -> GPIO5
VCC -> VBUS (5V)
GND -> GND
Before building the project, you need to have the following installed:
- Rust toolchain:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh- Target for ARM Cortex-M0+:
rustup target add thumbv6m-none-eabi- UF2 conversion tool:
cargo install elf2uf2-rs- Clone this repository:
git clone <repository-url>
cd flipclock- Build the project:
cargo build --release- The UF2 file will be created at:
target/thumbv6m-none-eabi/release/flipclock.uf2
- Flash to the Pico:
- Hold the BOOTSEL button on the Pico while connecting it to your computer
- Release the button once connected
- The Pico should appear as a USB mass storage device
- Copy the .uf2 file to the Pico drive
- The Pico will automatically restart and run the program
The motor speed can be adjusted by changing the rpm value in src/main.rs:
// Set motor speed to 15 RPM (revolutions per minute)
let rpm: f32 = 15.0;The 28BYJ-48 motor has:
- 4096 steps per revolution (in half-step mode, which this code uses)
- Gear ratio of 64:1
- Maximum practical speed of about 15-20 RPM
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under MIT License.
This template is intended as a starting point for developing your own firmware based on the rp2040-hal.
It includes all of the knurling-rs tooling as showcased in https://github.com/knurling-rs/app-template (defmt, defmt-rtt, panic-probe, flip-link) to make development as easy as possible.
probe-rs is configured as the default runner, so you can start your program as easy as
cargo run --releaseIf you aren't using a debugger (or want to use other debugging configurations), check out alternative runners for other options
-
The standard Rust tooling (cargo, rustup) which you can install from https://rustup.rs/
-
Toolchain support for the cortex-m0+ processors in the rp2040 (thumbv6m-none-eabi)
-
flip-link - this allows you to detect stack-overflows on the first core, which is the only supported target for now.
-
(by default) A
probe-rsinstallation -
A
probe-rscompatible probeYou can use a second Pico as a CMSIS-DAP debug probe. Details on other supported debug probes can be found in debug_probes.md
rustup target install thumbv6m-none-eabi
cargo install flip-link
# Installs the probe-rs tools, including probe-rs run, our recommended default runner
cargo install --locked probe-rs-tools
# If you want to use elf2uf2-rs instead, do...
cargo install --locked elf2uf2-rsIf you get the error binary `cargo-embed` already exists during installation of probe-rs, run cargo uninstall cargo-embed to uninstall your older version of cargo-embed before trying again.
For a debug build
cargo runFor a release build
cargo run --releaseIf you do not specify a DEFMT_LOG level, it will be set to debug.
That means println!(""), info!("") and debug!("") statements will be printed.
If you wish to override this, you can change it in .cargo/config.toml
[env]
DEFMT_LOG = "off"You can also set this inline (on Linux/MacOS)
DEFMT_LOG=trace cargo runor set the environment variable so that it applies to every cargo run call that follows:
export DEFMT_LOG=traceSetting the DEFMT_LOG level for the current session
for bash
export DEFMT_LOG=traceWindows users can only override DEFMT_LOG through config.toml
or by setting the environment variable as a separate step before calling cargo run
- cmd
set DEFMT_LOG=trace- powershell
$Env:DEFMT_LOG = tracecargo runIf you don't have a debug probe or if you want to do interactive debugging you can set up an alternative runner for cargo.
Some of the options for your runner are listed below:
-
cargo embedThis is basically a more configurable version ofprobe-rs run, our default runner. See thecargo-embedtool docs page for more information.Step 1 - Install
cargo-embed. This is part of theprobe-rstools:$ cargo install --locked probe-rs-toolsStep 2 - Update settings in Embed.toml
- The defaults are to flash, reset, and start a defmt logging session You can find all the settings and their meanings in the probe-rs repo
Step 3 - Use the command
cargo embed, which will compile the code, flash the device and start running the configuration specified in Embed.toml$ cargo embed --release -
probe-rs-debugger Step 1 - Install Visual Studio Code from https://code.visualstudio.com/
Step 2 - Install
probe-rs$ cargo install --locked probe-rs-toolsStep 3 - Open this project in VSCode
Step 4 - Install
debugger for probe-rsvia the VSCode extensions menu (View > Extensions)Step 5 - Launch a debug session by choosing
Run>Start Debugging(or press F5) -
Loading a UF2 over USB
Step 1 - Installelf2uf2-rs:$ cargo install elf2uf2-rs --lockedStep 2 - Modify
.cargo/configto change the default runner[target.`cfg(all(target-arch = "arm", target_os = "none"))`] runner = "elf2uf2-rs -d"
The all-Arm wildcard
'cfg(all(target_arch = "arm", target_os = "none"))'is used by default in the template files, but may also be replaced bythumbv6m-none-eabi.Step 3 - Boot your RP2040 into "USB Bootloader mode", typically by rebooting whilst holding some kind of "Boot Select" button. On Linux, you will also need to 'mount' the device, like you would a USB Thumb Drive.
Step 4 - Use
cargo run, which will compile the code and start the specified 'runner'. As the 'runner' is theelf2uf2-rstool, it will build a UF2 file and copy it to your RP2040.$ cargo run --release -
Loading with picotool
As ELF files produced by compiling Rust code are completely compatible with ELF files produced by compiling C or C++ code, you can also use the Raspberry Pi tool picotool. The only thing to be aware of is that picotool expects your ELF files to have a.elfextension, and by default Rust does not give the ELF files any extension. You can fix this by simply renaming the file.This means you can't easily use it as a cargo runner - yet.
Also of note is that the special pico-sdk macros which hide information in the ELF file in a way that
picotool infocan read it out, are not supported in Rust. An alternative is TBC.
The second-stage boot loader must be written to the .boot2 section. That
is usually handled by the board support package (e.g.rp-pico). If you don't use
one, you should initialize the boot loader manually. This can be done by adding the
following to the beginning of main.rs:
use rp2040_boot2;
#[link_section = ".boot2"]
#[used]
pub static BOOT_LOADER: [u8; 256] = rp2040_boot2::BOOT_LOADER_W25Q080;There are several feature flags in rp2040-hal.
If you want to enable some of them, uncomment the rp2040-hal dependency in Cargo.toml and add the
desired feature flags there. For example, to enable ROM functions for f64 math using the feature rom-v2-intrinsics:
rp2040-hal = { version="0.10", features=["rt", "critical-section-impl", "rom-v2-intrinsics"] }