Tuesday, August 4, 2015

Motorshield L293D

How to set up HL293D Motorshield with Arduino:
by J.B. Wylzan


Project 23:  L293D Motorshield

This project controls 2 dc motors to go Forward, Backward, Left, Right, Fast, Slow and Stop. To simulate the motors behaviors write a sketch that will turn On and Off 4 leds based on the table below. The program will simulate the forward (FWD), backward (BCK), right (RHT), left (LFT), and stop (STP) motions. Incorporate the Serial.println(" ")  on your sketch to guide you when the motor is moving forward, backward, left, right  or stop.
Use this model first to test your motors by using 4 leds and

L239D Configuration:  
The Device is a monolithic integrated high voltage,high current four channel driver designed to accept standard DTL or TTL logic levels and drive inductive loads (such as relays solenoides, DC and stepping motors) and switching power transistors .It has two bridges where each pair of channels is equipped with an enable input. A separate supply input is provided for the logic, allowing operation at a lower voltage and internal clamp diodes are included. This device is suitable for use in switching applications at frequencies up to 5 kHz. The new L293D motor shield has an input voltage *DC4.5-25V, *600mA OUTPUT CURRENT CAPABILITY PER CHANNEL *1.2A PEAK OUTPUT CURRENT (non repetitive)PER CHANNEL *ENABLE FACILITY *OVERTEMPERATURE PROTECTION *LOGICAL "0" INPUT VOLTAGE UP TO 1.5 V(HIGH NOISE IMMUNITY) *INTERNAL CLAMP DIODES. (source:Texas Instrument)

Pin Layout: 

Block Diagram:
Image designed using Fritzing

Hardware:
1 motorshield; L293D 
1 prototype shield
1 ultrasonic sensor
connecting wires
Arduino R3 UNO board

Code # 20:
/* ===============================================================
      Project # 20: Motor Shield l293D
      Author: J. B. Wylzan with some help from the Arduino community
      Website: http://www.ihackrobot.blogspot.com
      Abstract: This project shows how to turn On and Off motors
================================================================== */


const int trigPin 7
const int echoPin 6
#define ENA 3
#define IN1 5
#define IN2 4

// ENABLE PWM 3,5,6,9,10,11

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);    //setup sonic
  pinMode(echoPin, INPUT);
  pinMode(ENA,OUTPUT);         // setup motor A
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
   
}

void loop() {
  long time, int range;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  time = pulseIn(echoPin, HIGH);
  range = time*0.034/2;


  if (range <= 30 || range <= 0){
   
   digitalWrite(ENA,HIGH); // enable on
    digitalWrite(IN1,HIGH); //forward
    digitalWrite(IN2,LOW);  //disengage
    analogWrite(3, 255);    //speed
      delay(3000);
       digitalWrite(IN2,HIGH);   // engage
      delay(1000);
     digitalWrite(IN1,LOW);      // stop
    digitalWrite(IN2,LOW);       // break
    analogWrite(3, 123);         // slow
    
    
    Serial.print(range);
    Serial.println(" cm");
    delay(1000);
  
  }
  else {
      Serial.println(" cm");
      digitalWrite(IN1,LOW);  //stop
      digitalWrite(IN2,LOW);  //stop
      delay(1000);
  }
   
}
// end of the sketch


Challenge: Use the ultrasonic sensor to activate 2 dc motors or 2 wheels of a toy car using the motorshield. Modify code#20 sketch by adding the ultrasonic sketch.



Physical Layout:


Procedure:
1. Build the model as shown above
2. Run the Arduino Interface
3. Select File > New
4. Copy Code #20 above
5. Paste Code #20
6. Click File > Save
7. Click Verify
8. Click Upload
9. Lights in the motorshield will turn on and off. If connected to a small dc motor, the motor wheel move forward and in reverse.


For Stepper Motor:
int Pin0 = 8;
int Pin1 = 9;
int Pin2 = 10;
int Pin3 = 11;
int _step = 0;
boolean dir = true;
void setup()
{
pinMode(Pin0, OUTPUT);
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
}
void loop()
{
switch(_step){
case 0:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
break;
case 1:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, HIGH);
break;
case 2:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
break;
case 3:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
break;
case 4:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 5:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 6:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
case 7:
digitalWrite(Pin0, HIGH);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
break;
default:
digitalWrite(Pin0, LOW);
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
break;
}
if(dir){
_step++;
}else{
_step--;
}
if(_step>7){
_step=0;
}
if(_step<0){
_step=7;
}
delay(1);

}


Project # 21:  Bluetooth HC-05


Disclaimer:  We shall not be liable for any loss or damage of whatever nature - direct, indirect, consequential, or otherwise - which may arise as a result of your use of any information on this website. However, if you are interested in using any of the projects for personal or educational purposes, please inform the author by email. 

Public Domain Notice: Copyright (c) 2000. All rights reserved. This article is part of a book entitled iHackRobot. Copies are welcome to be shared or distributed publicly as long proper citations are observed. Please cite as follows: Biotronics: The Silver Species, Joey Lawsin, 1988, USA.

================================================================== 
The Homotronics® and Homodruinos® logos are registered trademarks.
Copyright Biotronics© Inc. iHackRobot®. All Rights Reserved.
Patent Pending. 2000 © ®

L.A.W.S.I.N. Educational Production
 ================================================================== 




No comments:

Post a Comment