Arduino Z

This Arduino is a two-state machine just like the previous one. It listens to two push buttons (PIN 2 and PIN 3) and a two bit message (via PIN 4 and PIN 5) from the Raspberry Pi. Either the push buttons or the Raspberry Pi message can trigger movement up or down the Z axis.


Sensory inputs: (i) Raspberry message and (ii) push (up/down) buttons

i.- Raspberry message As with XY, the Z axis can be controlled from the output pins of the Raspberry Pi by using two-bit messages (i.e. two pins). The ground state of the system is not moving. A state-put message can be represented as (0,0) while the moving up  and moving down messages are represented as (1,0) and (0,1) respectively. To listen to these messages we set  PIN 4and PIN 5 as digital input pis; to have a two-bit word to listen to the Raspberry Pi wishes for the Z axis.

ii.- Push (up/down) buttons HomeScope has two push-down buttons for direct user interaction. These buttons have external pull-up resistors so when clicked, the buttons are transiently connected to ground and trigger a signal the Arduino code detects as a pushed button event. This signal is a transient LOW voltage on PIN 2 orPIN 3 which code for up and down signals.

As soon as any of these situations is detected while on listening mode, the Arduino program enters into actuating mode and stops listening. In the actuating state, the code instruct the motor to move up or down as defined during listening mode.


Actuation outputs:  (i) Z stage movement (stepper control),  (ii) Signal back to the Raspberry.

i.- Z stage movement Stage movement is achieved by using two digital output pins (PIN 8 and PIN 9) to send the direction and step signals to the EasyDriver controlling the stepper.

ii.- Signal back to Raspberry For the Raspberry Pi to know if the Arduino is listening or bussy actuating a stepper movement, we mirror the machine state to output PIN 12. This way we can inform the Raspberry Pi that the Arduino is busy moving the stepper.


The code in the Arduino_Z.inofile controls the Z axis movement. We start defining a single variable DISTANCE to represent a unitary step size; this is to achieve the maximum possible resolution consisting on a single step per actuation cycle.

#define DISTANCE 1
int StepCounter =0;
int Stepping =false;

We also define a signal_pin  (PIN 12) that Arduino uses to signal back to the Raspberry Pi what state it is in (actuation or listening).

// declare flag pin to send digital output to the RASPBERRY PI
int signal_pin =12;

In the setup function we define output (8,9) and input (2,3,4,5) pins as well as writting the default value (0,0) message to be sent to the EasyDriver over PINS 8 and 9.

void setup()
pinMode(8, OUTPUT); // to ctrl direction
pinMode(9, OUTPUT); // to step one step
digitalWrite(8, LOW);// no-step
digitalWrite(9, LOW); // direction
pinMode(2, INPUT); // physical button step-up
pinMode(4, INPUT); // GUI button step-up
pinMode(3, INPUT); // physical button step-down
pinMode(5, INPUT);// GUI button step-down

Finally, the loopfunction has two if statements checking for move up or move down messages coming from the push buttons (PINS 2 and 3) or from the Raspberry Pi (PINS 4 and 5). In case one of such movement requests is detected, the machine switches from listening to actuating. Then, a third and fourth (nested) if statements take care of actuation.

void loop()
  if((digitalRead(2) == LOW || digitalRead(4) == HIGH) && Stepping == false)
       digitalWrite(8, LOW);
       Stepping = true;

  if((digitalRead(3) == LOW || digitalRead(5) == HIGH)  &&  Stepping == false)
      digitalWrite(8, HIGH);
      Stepping = true;

  if(Stepping == true) 
     digitalWrite(signal_pin, HIGH); // actuating
     digitalWrite(9, HIGH);
     digitalWrite(9, LOW);
     StepCounter = StepCounter +1;
     if(StepCounter == DISTANCE)
         StepCounter =0;
         Stepping = false;
         digitalWrite(signal_pin, LOW); // not actuating

The code running on this Arduino controls a single stepper motor and provides a single degree of freedom which HomeScope uses to focus the RaspiCAM sensor at the sample plane.  This focus movement (Z axis) is powered by a Nema 17 stepper motor of 200 steps which is controlled by an EasyDriver which receives two-bit instructions from the Arduino via PIN 8 (direction) and PIN 9 (step).