Skip to content

4. Embedded programming

steppermotor

Overview of week 4 assignment

  1. Group assignment
    1. demonstrate and compare the toolchains and development workflows for available embedded architectures
  2. Individual assignment
    1. browse through the data sheet for your microcontroller
    2. write a program for a microcontroller, and simulate its operation to interact (with local input &/or output devices) and communicate (with remote wired or wireless connections)
    3. extra credit: test it on a development board extra credit: try different languages &/or development environments

1. Group assignment

For more information, see the Week 04: Group assignment page.

2. Indiidual assignment

A. Microcontroller RP2040 datasheet

I read through RP2040 datasheet, while considering required microcontroller specification for my Final project, mobile 3D printer with infinite print size.


a. General info

  1. RP2040 means:
    • RP = Raspberry Pi
    • 2 = Numbers of cores: 2 cores
    • 0 = Types of processor: M0+ architecture (a low-power 32-bit Arm microcontroller core)
    • 4 = RAM configuration: 264 KB of SRAM
    • 0 = No onboard nonvolatile storage
  2. Maximum clock speed: 133Mhz
    Moderate but fast as a microcontroller, and therefore, low-power.
    • More than enough for basic 3D printer.
  3. SRAM (Static Random-Access Memory): 264kB
    Relatively large as a microcontroller chip.
    • For 3D printer, SRAM is used for real-time control (sensor data, motor control, G-code buffering, user interface).
    • Enough for basic 3D printer.
  4. Onboard nonvolatile storage (flash memory): None
    • For 3D printer, G-code files, printer settings need to be stored non-volatilely.
    • External SPI flash memory, micro SD card or USB drive can be connected.
  5. 30 multifunction GPIO (General Purpose Input/Output)
    These can be configured as Digital input or output, UART, SPI, PWM, analog input, and PIO (Programmable I/O) for custom communication protocols.
    • Enough flexibility for 3D printer, can be used for stepper motor, endstop, thermostat, fan, user intergace and SD card interface such as an example below.
      • 6 Motors (with external motordrivers for microstepping): 6 motors × 2 pins = 12 GPIO pins.
      • 1 Limit Switch (Z-axis): 1 GPIO pin.
      • 1 Temperature Sensor: 1 GPIO pin.
      • 1 Pin for PWM Control (Extruder Heater): 1 GPIO pin.
      • 1 Pin for Cooling Fan Control: 1 GPIO pin.
      • 2 Pins for User Interface (Buttons, LCD screen, etc.): 2 GPIO pins.
      • 4 Pins for SD Card Interface (SPI Communication): 4 GPIO pins.
      • Total = 22 GPIO pins used.

b. Comparison with basic 3D printer microcontrollers

I asked ChatGPT to compare RP2040 with STM32-F103, microcontroller used for Kingroon KP3S 3D printer.

Prompt: Make a table to briefly compare the specifications of RP2040 and STM32-F103.

Feature RP2040 STM32F103
Core Dual-core ARM Cortex-M0+ (up to 133 MHz) Single-core ARM Cortex-M3 (up to 72 MHz)
RAM 264 KB SRAM 20 KB SRAM
Flash Memory No onboard flash (it supports external SPI flash) 64 KB or 128 KB onboard Flash
GPIO Pins 30 multifunction GPIO pins Up to 37 GPIO pins
Analog I/O 4 analog inputs (12-bit ADC) 10-bit ADC with up to 16 channels
PWM Pins Up to 16 PWM channels Up to 12 PWM channels
Communication Interfaces 2 × UART, 2 × SPI, 2 × I2C, 1 × USB 1.1, 1 × I2S 2 × UART, 2 × SPI, 1 × I2C, 1 × USB 2.0
Special Features Programmable I/O (PIO) CAN interface (optional), RTC
Low Power Modes Yes Yes

The RP2040 outperforms the STM32-F103 in almost all aspect, so it seems like the RP2040 is enough to build a basic 3D printer.


B. Programming for microcontroller

a. Simulator

I used WOKWI, online simulator for electronics. Since I tried to use Xiao RP2040 using Arduino language (C++), I selected "Pi Pico" (it uses RP2040) starter template, and tried the "hello.button-blink" code demonstrated in the class. I encountered 3 errors, partly;

  1. Error: Serial speed was not defined, Serial.begin() > Serial.begin(115200)
  2. Serial print was garbled, it was fixed by changing all Serial > Serial1 (It seems like changing Serial to Serial1 only work in WOKWI simulator, initial Serial worked for the actual board.)
  3. Serial print was endlessly printed without pushing button, I asked ChatGPT why that could be occur. Then I added delay(50); after push button.

wowki

For my undestanding, I also asked ChatGPT to add explanations in the code below.

Prompt: Please explain line by line and write comments within the code.


b. Test a program on development board

  1. UF2 bootloader: Following RP2040 Pinout and firmware, I initilized the RP2040 for Arduino by using bootloader "flash_nuke.uf2"

    Bootloader is a small program in a microcontroller that runs at startup. It allows easy firmware updates without special hardware, helps recover from crashes, and can add security features. It works by checking for new code before running the main program.

    • Connect RP2040 to laptop
    • Push and hold the BOOT button and RESET button
    • Once the RP2040 recognized as USB mass storage device, copy the uf2 file into it
    • Automatically initialized and ejected

    RP2040

  2. Setting board and port in Arduino:

    • Tools > Board > Board manager > Search "RP2040" and install it
    • Tools > Board > ...select Raspberry Pi Pico
    • Tools > Port > ...select port
  3. Wiring: In WOKWI, I simulated the push button blinking code as a Raspberry Pi Pico, but because the pin arrangements are different between the Raspberry Pi Pico and the RP2040, I changed the wiring.

    wiring

Note

I have wasted 20-30 minutes on simple wiring mistakes! Double check wiring!


C. Stepper motor test

Since my final project will use stepper motor, I tried to use stepper motor.

a. Program

I found a sample program, pi-pico-stepper.ino in WOKWI, and tried to understand the code below. This code will rotate the motor 360° clockwise in 1 second, wait 1 second, and then repeat the cycle.

The most important part of this code is this for loop.

  • for (int i = 0; i < 200; i++) {} the following action is repeated 200 times to rotate 360°.
    • digitalWrite(STEP_PIN, HIGH) set the step signal to HIGH, move forward 1 step (1.8°)
    • digitalWrite(STEP_PIN, LOW) set the step signal to LOW, prepare for the next step
    • delay(5) wait 5ms, determines the rotation speed.
  • digitalWrite(DIR_PIN, HIGH) set motor direction to clockwise
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Define the direction control pin (DIR_PIN) for the stepper 
#define DIR_PIN 0
#define STEP_PIN 1

void setup() {
  pinMode(STEP_PIN, OUTPUT); // Set STEP_PIN as an output
  pinMode(DIR_PIN, OUTPUT);  // Set DIR_PIN as an output
  digitalWrite(STEP_PIN, LOW); // Initialize the step signal as LOW
  digitalWrite(DIR_PIN, LOW); // Initialize the step signal as LOW
}

void loop() {
  digitalWrite(DIR_PIN, HIGH); // Set motor direction to clockwise (CW)

  for (int i = 0; i < 200; i++) { // Loop 200 times (one full revolution)
    digitalWrite(STEP_PIN, HIGH); // Set the step signal HIGH (trigger one step)
    digitalWrite(STEP_PIN, LOW);  // Set the step signal LOW (prepare for next step)
    delay(5); // Wait 5 ms per step (5ms * 200 = 1000ms = 1 second per rotation)
  }

  digitalWrite(DIR_PIN, LOW); // Set motor direction to counter clockwise (CCW)

  for (int i = 0; i < 200; i++) { // Loop 200 times (one full revolution)
    digitalWrite(STEP_PIN, HIGH); // Set the step signal HIGH (trigger one step)
    digitalWrite(STEP_PIN, LOW);  // Set the step signal LOW (prepare for next step)
    delay(25); // Wait 5 ms per step (5ms * 200 = 1000ms = 1 second per rotation)
  }

  delay(1000); // Wait 1 second before repeating the cycle
}

b. Electronic components

I used the following parts and connected them on a breadboard. This is a simple setup and is not recommended for long time use.

  • NEMA17 Stepper Motor A motor that can be precisely controlled (commonly used in robotics and CNC machines). I took this motor by disassembling my old 3D printer, Kingroon KP3S.
  • A4988 Stepper Motor Driver Controls the movement of the NEMA17 motor by converting signals from the Pico.
  • Breadboard Power Module Supplies power to the Pico and A4988 (stabilizes 5V power from the PC's USB).
  • Electrolytic Capacitor (100µF) Stabilizes power to the A4988 and reduces noise.

c. Wiring

d. Appendix: How stepper motor works?


3. Files

Afterthoughts

  • After struggling with many simple mistakes in the middle of the night, seeing the LED blink was...an emotional experience.
  • Even after researching, I still couldn't imagine how to control a stepper motor, but gradually I began to feel like I might be able to do it.
  • To enhance my time management and documentation, I started using a whiteboard. Since I am a visual person, it helps me understand abstract concepts more clearly by representing them visually. It also allows me to keep track of what I’ve done, what I’m working on, and what I plan to do.

whiteboard