Sunday, August 16, 2015

MicroServo SG90

How to pair HC-05 Bluetooth on an Arduino with an Android phone:
by J.B. Wylzan


Project 25:  Microservo SG90

This project shows how to control the microservo from 0 to 180 degrees.





Hardware:
microservo SG90
connecting wires
breadboard
Arduino R3 UNO board


Block Diagram:




NOTE: The RGB and the Push Button are not on the sketch below. However using your understanding on our previous lessons or projects, you can add them on your sketch. Good Luck ;-)

Code # 22:

/* ===============================================================
      Project # 27: Microservo SG90
      Author: J. B. Wylzan with some help from the Arduino community
      Website: http://www.ihacklab.blogspot.com
      Abstract: This project shows how to activate a servo
================================================================== */


#include <servo .h>
int x = 1000

Servo servo;                         // name the Servo

void setup()
{
   servo.attach(6);                 // servo pin to arduino pin 6
}

void loop()
{
    servo.write(90);                  // Turn servo back to 90 center position
   delay(x);                              // Wait 1 second
   servo.write(45);                   // Turn servo Left to 45 degrees
   delay(x);                              // Wait 1 second
   servo.write(90);                   // Turn servo back to center position
   delay(x);                              // Wait 1 second
   servo.write(135);                 // Turn servo Right to 135 degrees
   delay(x);                              // Wait 1 second
   servo.write(0);                     // Turn servo Left to 0 degrees
   delay(x);                             // Wait 1 second
   servo.write(180);                // Turn servo Right to 180 degrees
   delay(x);                             // Wait 1 second
}




Challenge: 

Create the robot below using the microservo with an ultrasonic sensor attached on its axle.



Actual Layout:


Procedure:
1. Build the prototype as shown above
2. Run the Arduino Interface
3. Select File > New
4. Copy Code #22 above
5. Paste Code #22
6. Click File > Save
7. Click Verify
8. Click Upload
9. Servo will scan from 0 to 180 degrees.

Serial Monitor Test:

int servopin=9;
int myangle;
int pulsewidth;
int val;

void servopulse(int servopin,int myangle)
{
pulsewidth=(myangle*11)+500;          // convert angle to 500-2480 pulse width
digitalWrite(servopin,HIGH);                // set the level of servo pin as “high”
delayMicroseconds(pulsewidth);        // delay microsecond of pulse width
digitalWrite(servopin,LOW);               // set the level of servo pin as “low”
delay(20-pulsewidth/1000);
}

void setup()
{
pinMode(servopin,OUTPUT);
Serial.begin(9600);
Serial.println("servo ready" ) ;
}
void loop() {
val=Serial.read();
if(val>'0'&&val<='9')
{
val=val-'0';                                          // convert character to number
val=val*(180/9);                                // convert number to angle
Serial.print("servo angle: ");
Serial.print(val,DEC);
Serial.println();
for(int i=0;i<=50;i++) 
{
servopulse(servopin,val);
}
}

}










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. 


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
 ================================================================== 

Saturday, August 8, 2015

Bluetooth HC05

How to pair HC-05 Bluetooth on an Arduino with an Android phone:
by J.B. Wylzan


Project 24:  Bluetooth HC-05

This project pairs the HC05 transceiver with an android phone
and lights an LED on and off.




WARNING: Before you proceed, please read the HC-05 datasheetFrom this time on forward, specially when we start connecting wireless through our bluetooth, ethernet, motorshield, and internet, make sure to read first the warning signs. There are times you might change your modem settings automatically accidentally, burn your PC as your internal temperature gets higher unknowingly, or even hack your neighbors internet unintentionally. So please BEWARE. If you are not sure, the best place to get help is the Arduino Forum.


HC-05 Circuit Wiring:  
   HC05 Gnd      - Arduino pin Gnd
   HC05 3.3        - Arduino pin 3.3v
   HC05 Tx         - Arduino pin 1 (Tx)
   HC05 Rx         - Arduino pin 0 (Rx)
  
Block Diagram:

Circuit config for AT commands
Hardware:
HC05 bluetooth RF

Arduino phone (samsung galaxy) 9v battery (for pairing wireless)
connecting wires
breadboard
Arduino R3 UNO board

Code # 21a:
/*Pairing HC05 with a bluetooth Android phone or tablet

  using numerical inputs   The example code is public domain.*/

int ledPin = 13; 
int state = 0;
int flag = 0; 

void setup() {
 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, LOW);
 Serial.begin(9600); 
}

void loop() {

 if(Serial.available() > 0){
 state = Serial.read();
 flag=0;
 }

 if (state == '0') {
 digitalWrite(ledPin, LOW);
 if(flag == 0){
 Serial.println("LED: off");
 flag = 1;
 }
 }

 else if (state == '1') {
 digitalWrite(ledPin, HIGH);
 if(flag == 0){
 Serial.println("LED: on");
 flag = 1;
 }
 }
}
// end of the sketch


Code # 21b:

/* Pairing HC05 with a bluetooth Android phone or tablet
using characters inputs  The example code is public domain. */

int ledPin = 13; 
char kim;
String inputString="";

void setup() {
 pinMode(ledPin, OUTPUT);
 Serial.begin(9600); 
}

void loop() {
 if(Serial.available()) {
   while(Serial.available())
   {
 char inChar = (char)Serial.read();
 inputString += inChar;
 }
Serial.println(inputString);
while(Serial.available()>0)
{kim=Serial.read();}
 if (inputString == "q") {
 digitalWrite(ledPin, HIGH);
 }else if (inputString == "p"){
   digitalWrite(ledPin, LOW);
 }
 inputString = "";
 }
}
// end of the sketch

Actual Setup:
Serial Monitor and Bluetooth Terminal
Procedure:
1. Build the prototype as shown above
2. Run the Arduino Interface
3. Select File > New
4. Copy Code #21 above
5. Paste Code #21
6. Click File > Save
7. Click Verify
8. Click Upload
9. Open Serial Monitor
10. Type 1 or 0 / q or p on the serial motor command line 
11. Press Enter - you should see the Led turns On or Off

How to pair your Bluetooth with your android phone:
1. Go to Settings and select Bluetooth
2. Click Scan to detect your bluetooth
3. Select HC05 on the available devices list
4. Enter the device pin (0000 or 1234)
5. Click OK and the led blinking will settle down in series of two counts
6. Download the Bluetooth Terminal apps from Google Play
7. Open and select HCO5 on the list
8. If ask to connect, click Connect
9. The codes are transmitted wirelessly on your phone.

Challenge:
Now do the opposite. Use your phone to control the led on and off wireless.

Common AT Commands:
1. AT+VERSION
2. AT+NAME
3. AT+PSWD
4. AT+UART
5. AT+ROLE

Resources: AT Commands PDF


Project # 22:  MicroServo SG90


// Type 1,2,3,on the serial monitor to turn two leds on or off. (watch2AFNL)

// Read data from the serial and turn ON or OFF a light depending on the value
//#include <SoftwareSerial.h>

//SoftwareSerial mySerial(0,1); // RX, TX


char val;                                              // Data received from the serial port
int ledPin1 = 4;                
int ledPin2 = 10;

void setup() {
  pinMode(ledPin1, OUTPUT);         // Set pin as OUTPUT
  pinMode(ledPin2, OUTPUT);
  Serial.begin(9600);                         // Start serial communication at 9600 bps
}

void loop() {
  if (Serial.available()) {          
    int data = Serial.read();            
    switch (data) {
case '1':                  
    digitalWrite(ledPin1, HIGH);    //led1 is ON
    break;
case '2' :          
    digitalWrite(ledPin2, HIGH);    //led2 is ON
    break;
 
case '3' :          
    digitalWrite(ledPin1, LOW);    //led1 is OFF
    digitalWrite(ledPin2, LOW);    //led2 is OFF
    break;

default :
Serial.println("TYPE 1, 2 or 3 ");
break;

}
  }
}

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
 ================================================================== 




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
 ==================================================================