Writings - Oscilloscope and Stepper Motor Fun

Niek Sanders, July 2021

A week ago I purchased a Rigol 1054Z oscilloscope. Since then I've had fun measuring all sorts of stuff: bounce in switches, the PWM outputs of my Pi Pico, voltage spikes in a 555 astable timer, etc.

I decided to visualize how my Raspberry Pi Pico controls a 28BYJ-48 stepper motor. I already had code laying around, and I thought it worked fine. This was my first opportunity to use all four probes at once.

Experiment setup of stepper wired to oscilloscope.

It was immediately evident that something was wrong. My "working" C code should have been bringing one input high at a time.

Incorrect signal output on scope, 4 channel view.

The happily spinning motor had misled me into believing my code was correct. The sadness is clearer when turning off two channels:

Incorrect signal output on scope, 2 channel view.

Squinting at my code, the culprit was obvious. I'd accidently shadowed the int cur = 0; inside my while loop. This is also where I discovered that the CMake example I'd used didn't include -Wall by default.

// configure GPIO pins for output uint pmask = (1<<IN1) | (1<<IN2) | (1<<IN3) | (1<<IN4); gpio_init_mask(pmask); gpio_set_dir_out_masked(pmask); // initial energized state int cur = 0; gpio_put(pins[cur], GPIO_ON); while ( true ) { sleep_ms(3); int pre = cur; int cur = (cur + 1) % 4; uint toggle_mask = (1<<pins[pre]) | (1<<pins[cur]); gpio_xor_mask(toggle_mask); }

I removed the errant int specifier. Lo, with the code fixed, the scope looks like you'd expect:

Correct signal output on scope, 4 channel view.

This was my first real defect debugged with a scope.

>>> Writings