Robert Cudmore    archive    tags    search    software

Arduino + stepper motor + rotary encoder + scan image frame clock + serial logging

Goal here is to make an Arduino do four things simultaneously:

  1. Read a rotary encoder
  2. Drive a stepper motor
  3. Respond to Scan Image (SI) frame clock
  4. Log all events to serial

The rotary encoder and stepper motor (1,2) need to be non-blocking. All inputs (1,2,3) need to use low-level interrupts to be ‘timing precise’ and not get blocked themselves.

Initially I wanted to log all this to Arduino memory but there is not enough on the Arduino Uno. Upgrading to Arduino Mega gives you 4x memory but still not enough. The rotary encoder fires off tons of events and if I hard code events as an array (in Arduino memory), I am limited to about 250 events. Same problem with SI frames, I am limited to about 250 frame timestamps. Arduino ProgMem does not work as it is not writable at runtime. In the future I may get an SD card shield for this?

For now I am having Arduino log all events to serial and am using python code (on a Raspberry Pi) to grab and save all serial events coming from Arduino. This seems to work well. The critical piece is that the serial logger on the Pi is stateless, you just ‘set it and forget it’ and it logs all events all day.

Install Arduino 1.6.x

Don’t upgrade the system to Debian Jesse, just pull the Arduino 1.6 packages from Debian Jesse repository.

His site is moving to Another person who bailed on Wordpress, hard times for wordpress?

Motor controller

I am using EasyDriver

Non blocking stepper motor on arduino

AccelStepper Arduino library.

Rotary encoder

I have a ‘Honeywell 600-128-cbl’. Looking at wires as they come out of encoder…

Pinouts are in .pdf on farnell website

Mount with nut and lockwasher

Rotary encoder library

A non-blocking rotary encoder library for Arduino is made by pjrc.

Setting arbitrary interrupts on Arduino

By default the Arduino Uno has low level interrupts on input pins 2 and 3. I need more interrupts. You can upgrade your Arduino board or just set low level interrupts on any input pin. I am setting A0/A1/A2 to accept low level interrupts

Plotting sensor data from arduino

This is tricky as parsing incoming serial data is slow. It is slow on any operating system and with any CPU chip including Pentium i7 running OSX or Window, Arm processor on Raspberry Pi. The serial parsing/reading eventually finishes but to get a legitimate real-time plot requires buffering a number of values and plotting them in a batch. This is particularly important for the high bandwidth of the rotary encoder.

I tried three different options and dedicating myself to pyqtgraph…

Good code for python matplotlib

Processing on the pi

Installing Processing on the PI is a little tricky.


sudo pip install pyqtgraph


Put Arduino libraries in

Tags: arduino, python, raspberry pi

©2018. Robert Cudmore.