NKC Electronics

NKC Electronics Support, Project and Discussion Forums
It is currently Sun Jun 25, 2017 9:03 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Arduino motor shield R3
PostPosted: Mon Jul 02, 2012 10:47 am 
Offline

Joined: Thu Sep 08, 2011 9:59 pm
Posts: 71
This is the new motor shield from the Arduino Team. It is relatively new and buyers are having difficulty to find good examples, especially how this shield can be used to drive stepper motors.

I found the following sample sketch to drive a stepper motor (published by Kris Arnold http://krisarnold.com/2012/02/03/arduino-and-the-official-motor-shield-r3/)

Code:
// Include the Stepper Library
#include <Stepper.h>

// Map our pins to constants to make things easier to keep track of
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

// The amount of steps for a full revolution of your motor.
// 360 / stepAngle
const int STEPS = 48;

// Initialize the Stepper class
Stepper myStepper(STEPS, dirA, dirB);

void setup() {
  // Set the RPM of the motor
  myStepper.setSpeed(30);

  // Turn on pulse width modulation
  pinMode(pwmA, OUTPUT);
  digitalWrite(pwmA, HIGH);
  pinMode(pwmB, OUTPUT);
  digitalWrite(pwmB, HIGH);

  // Turn off the brakes
  pinMode(brakeA, OUTPUT);
  digitalWrite(brakeA, LOW);
  pinMode(brakeB, OUTPUT);
  digitalWrite(brakeB, LOW);

  // Log some shit
  Serial.begin(9600);
}

void loop() {
  // Move the motor X amount of steps
  myStepper.step(STEPS);
  Serial.println(STEPS);
  // Pause
  delay(2000);

  // Move the motor X amount of steps the other way
  myStepper.step(-STEPS);
  Serial.println(-STEPS);
  // Pause
  delay(2000);
}


I will be doing more tests and posting more information about this product.


Top
 Profile  
 
PostPosted: Tue Jul 03, 2012 7:32 pm 
Offline

Joined: Thu Sep 08, 2011 9:59 pm
Posts: 71
This is a simple sketch that shows how to drive a DC motor using the Arduino Motor Shield R3.

Connect a DC motor to Motor Output A (blue terminal block, mid 2 screws). Connect motor power supply to Vin (Arduino or VIN terminal block, lower 2 screws). If your motor exceeds 12V, it is recommended to cut the VIN-CONNECT jumper. By default, VIN-CONNECT is connected at it means will send whatever you put on VIN on the Motor Shield to the Arduino board, which is not desirable. If your VIN requirement for the motor is in the safe range for the Arduino board (7 to 12V), then you are OK.

The Arduino Motor Shield R3 has some nice features, like a BREAK pin to stop the motor and how much current the board is sending to the board, which can be read at Analog In 0 and 1 (motor A and motor B). The motor driver supports up to 2A per channel. The range is calibrated for 2A to be 3.3V. Reading the voltage on A0 and A1, you can check how much current the motor is taking. This sketch will simply use the break function and I will leave the current reading for another post.

Run the following sketch:

Code:
// Sketch to drive a simple DC motor
//
// Motor connected to Motor output A on Arduino Motor Shield R3
// This example uses the output A.  If you want to use output B or a 2nd motor
// just change DIR_A for DIR_B, SPEED_A for SPEED_B, BREAK_A for BREAK_B
//
// NKC Electronics 2012

// Motor A definitions
int DIR_A = 12;
int SPEED_A = 3;
int BREAK_A = 9;

// Motor B definitions
int DIR_B = 13;
int SPEED_B = 11;
int BREAK_B = 8;

void setup()
{
  pinMode(SPEED_A, OUTPUT);
  pinMode(DIR_A, OUTPUT);
  pinMode(BREAK_A, OUTPUT);
 
  // release break
  digitalWrite(BREAK_A, LOW);
}

void loop()
{
  // move motor forward mid speed.  Max is 250.
  digitalWrite(DIR_A, HIGH);
  analogWrite(SPEED_A, 125);
  // wait 5 seconds
  delay(5000);
 
  // break the motor
  digitalWrite(BREAK_A, HIGH);
  delay(100);
  // release break.  If BREAK_A continues to be HIGH, the motor will not move
  digitalWrite(BREAK_A, LOW);
 
  // move motor backward mid speed.  Max is 250.
  digitalWrite(DIR_A, LOW);
  analogWrite(SPEED_A, 125);
  // wait 5 seconds
  delay(5000);
 
  // break the motor
  digitalWrite(BREAK_A, HIGH);
  delay(100);
  // release break.  If BREAK_A continues to be HIGH, the motor will not move
  digitalWrite(BREAK_A, LOW);
}


Top
 Profile  
 
PostPosted: Tue Jul 03, 2012 11:10 pm 
Offline

Joined: Thu Sep 08, 2011 9:59 pm
Posts: 71
The Arduino Motor Shield R3 has a current sensing feature on Analog In 0 and 1.

The scale is 1.65V/A (1.65V reading per Ampere)

Reading A0 and A1, you can check how much current is flowing to the motors. 3.3V reading is 2A.

Here is an example sketch that starts a motor at low speed, then reads the current. Then increases the speed to a mid range, and reads the current again, then it goes full speed and does a final reading of the current.

The current output is displayed on a Serial LCD that uses only 1 pin on the Arduino (http://store.nkcelectronics.com/16x2-Serial-LCD-Module-Blue-with-White-backlight_p_347.html)

Code:
// Sketch to test current sensing feature on the Arduino Motor Shield R3
//
// Motor connected to Motor output A on Arduino Motor Shield R3
// Serial LCD is connected to digital pin 1 (TX).
// This example uses the output A.  If you want to use output B or a 2nd motor
// just change DIR_A for DIR_B, SPEED_A for SPEED_B, BREAK_A for BREAK_B
//
// Sensing current of Motor A on Analog In 0.  3.3V reading is 2A, 1.65 is 1A, etc
//
// NKC Electronics 2012

// Motor A definitions
int DIR_A = 12;
int SPEED_A = 3;
int BREAK_A = 9;

// Motor B definitions
int DIR_B = 13;
int SPEED_B = 11;
int BREAK_B = 8;

// Escape character for Serial LCD
char ESC = 0xFE;

void setup()
{
  pinMode(SPEED_A, OUTPUT);
  pinMode(DIR_A, OUTPUT);
  pinMode(BREAK_A, OUTPUT);
 
  // release break
  digitalWrite(BREAK_A, LOW);
 
  // Initialize serial communication with the LCD
  Serial.begin(9600);
 
  // Initialize LCD module
  Serial.write(ESC);
  Serial.write(0x41);
  Serial.write(ESC);
  Serial.write(0x51);
  // Set Contrast
  Serial.write(ESC);
  Serial.write(0x52);
  Serial.write(40);
  // Set Backlight
  Serial.write(ESC);
  Serial.write(0x53);
  Serial.write(8);
  Serial.print("NKC Motor Test");
}

void loop()
{
  int i, current, c;
 
  for (i=50; i<=150; i+=25) {
    // move motor forward at varying speed.
    digitalWrite(DIR_A, HIGH);
    analogWrite(SPEED_A, i);
    delay(500);
 
    // Read current from A0 (motor A)
    current = analogRead(0);

    // map input voltage to 0-2000mA range
    c = map(current, 0, 676, 0, 2000);

    // Set cursor line 2, column 0 and display current (mA)
    Serial.write(ESC);
    Serial.write(0x45);
    Serial.write(0x40);
    Serial.print("current = ");
    Serial.print(c);
    Serial.print(" mA    ");
    delay(4000);
 
    // break the motor
    digitalWrite(BREAK_A, HIGH);
    delay(500);

    // release break.  If BREAK_A continues to be HIGH, the motor will not move
    digitalWrite(BREAK_A, LOW);
  }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group