Wednesday, January 29, 2020

Breadboarding and Fritzing

I had an interesting situation presented to me last week when a couple of Grade 7 students asked for help with Arduino. Sensing a greater play at work I asked what they were trying to accomplish and it turns out they had purchased a NEMA17 1.7amp stepper motor from Amazon and wanted to make a moving arm model for science fair.

It took me a few seconds to gather my thoughts but it turns out they had a family friend who had constructed one similar and suggested to them to acquire the necessary materials. Unfortunately, said friends neglected to over Arduino basics, breadboarding or stepper motors controls.

It gave me an opportunity to revisit the Arduino and pinning out of various components and modules, and also gave me opportunity to play with Fritzing, a handy breadboarding/circuit diagram planning program. Note that there is a Homebrew Formulae for Fritzing at: /api/cask/fritzing.json

I ordered some A4988 stepper drivers and Easydrivers and set it all up on a small board. Inputted some quick code (see below) and printed out the diagram:




/*this code controls a stepper motor
 * it uses two push buttons on a breadboard
 * one is forward and the other is reverse
 * StepForward and StepBack tell the motor what to do when a button is pushed
 * adjust the x< value for rotations
 * adjust the delay for speed
 */
 
#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
#define EN  6

const int button1Pin = 7;
const int button2Pin = 8;
const int ledPin =  13;
int button1State = 0;
int button2State = 0;

char user_input;
int x;
int y;
int state;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
    
  pinMode(stp, OUTPUT);
  pinMode(dir, OUTPUT);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(EN, OUTPUT);
  resetEDPins();
}

void loop() {
      button1State = digitalRead(button1Pin);
      button2State = digitalRead(button2Pin);
      
      if (button1State == HIGH) {
        digitalWrite(EN, LOW);
        StepForward();
        resetEDPins();
        delay(1000);
      } 
      if (button2State == HIGH) {
        digitalWrite(EN, LOW);
        StepBack();
        resetEDPins();
        delay(1000);
      }else {
        digitalWrite(ledPin, LOW);
      }
}

void resetEDPins()
{
  digitalWrite(stp, LOW);
  digitalWrite(dir, LOW);
  digitalWrite(MS1, LOW);
  digitalWrite(MS2, LOW);
  digitalWrite(EN, HIGH);
}

void StepForward()
{
  digitalWrite(dir, LOW);

  for(x= 1; x<500; x++)
  {
        digitalWrite(stp,HIGH);
        delay(1);
        digitalWrite(stp,LOW);
        delay(1);
    }
}

void StepBack()
{
 digitalWrite(dir, HIGH);
  for(x= 1; x<1000; x++)
  {
    digitalWrite(stp,HIGH);
    delay(1);
    digitalWrite(stp,LOW);
    delay(1);
  }
}