Showing posts with label arduino. Show all posts
Showing posts with label arduino. Show all posts

Friday, 25 March 2016

MIT app inventor Control Multiple Arduino's PWM pins

Sebarang pertanyaan/pembelian/bantuan boleh dm saya dia twitter eh:
twitter.com/zakimuslim_

Theres many student ask me how to control more than 1 Arduino's  PWM pin using Visual Basic/Android App to control their project.

Okay today i will show you a trick how to control Arduino's PWM pins.

What is Pulse Width Modulation (PWM) ? Here a pic to explain PWM.



Check out this video how to coding:

 https://www.youtube.com/watch?v=U45pEQSm0-0


Download APK and Arduino code that i use in my video :)

1. APK = https://drive.google.com/open?id=0B3c_VPBO9Qq3dF9IdVh0X2kxWmc
2. AIA = https://drive.google.com/open?id=0B3c_VPBO9Qq3M01VUm9GYnJCbzA
3. Arduino Code = https://drive.google.com/open?id=0B3c_VPBO9Qq3TTE1bW9mUG1nLTQ




Actually i use a "String" function in Arduino IDE examples to create this program.
here:







This is my Arduino Sketch.

First i create a new string with name "fromAndroid".
and in void loop i start to analyse the string that i've send from android.
look at this:

if(Serial.available() > 0) 
  {
    fromAndroid = Serial.readString(); 

    if(fromAndroid.startsWith("pwm3"))   
    {
      fromAndroid.replace("pwm3", "");
      analogWrite(pwm3, fromAndroid.toInt());
      Serial.print("PWM3 VALUE: ");
      Serial.println(fromAndroid);
    }





let me explain this code.


if(Serial.available() > 0)
- i check if any string receive from android


 fromAndroid = Serial.readString(); 
if string is available, arduino will start read.


if(fromAndroid.startsWith("pwm3"))
- then i check in string its start with what character.  if in string is "pwm3255"  its means at pin pwm3 the value is 255.


fromAndroid.replace("pwm3", "");
-in Arduino we cannot write directly like this analogWrite(pin, pwm3 255). Because in the argument we only can write an integer or number. So we need to remove some character in the string. In this case, i have string with pwm3255 and i want to remove this "pwm3" to make sure i have only integer/number in my string.
i use this function that called string replacement.

stringName.replace("things want to replace" , "replace with what you want")
fromAndroid.replace("pwm3", " ");

i leave the second argument with space to erase the "pwm3"
Now the replacement is done! now we have "255" in the string :D


analogWrite(pwm3, fromAndroid.toInt());
-After replacement in string only have "255". As we know, to use analogWrite function we only can use integer or number only in the first or second argument. We cannot use char, string, or anything! only integer. Now what should i do, i use this function string.toInt() to convert from string to integer!  in our string now is "255" so just convert it directly by using this function. Just write like this:

analogWrite(pwm pin thats we want to write with , the string that we want to convert to integer);
analogWrite(pwm3, fromAndroid.toInt());

in first argument is PWM pin that we want to use, in second argument is the string we want to convert.



Okay lets see this another example:
if in string or android sends "pwm5150" its means we want to write value at pin PWM 5 is 150.

first analyse the the string using fromAndroid.startWith("pwm5")

then use replace function to erase the "pwm5" like this  fromAndroid.replace("pwm5", "");

and change the string to integer using .toInt() function  analogWrite(pwm5, fromAndroid.toInt());




Here code i've write and use in my video.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

String fromAndroid;

int pwm3 = 3;
int pwm5 = 5;
int pwm6 = 6;
int pwm9 = 9;
int pwm10 = 10;
int pwm11 = 11;

void setup() {
  Serial.begin(9600);
  Serial.println("Program Start..");
  pinMode(pwm3, OUTPUT);
  pinMode(pwm5, OUTPUT);
  pinMode(pwm6, OUTPUT);
  pinMode(pwm9, OUTPUT);
  pinMode(pwm10, OUTPUT);
  pinMode(pwm11, OUTPUT);

}

void loop() {
  if(Serial.available() > 0)
  {
    fromAndroid = Serial.readString();

    if(fromAndroid.startsWith("pwm3"))
    {
      fromAndroid.replace("pwm3", "");
      analogWrite(pwm3, fromAndroid.toInt());
      Serial.print("PWM3 VALUE: ");
      Serial.println(fromAndroid);
    }

    if(fromAndroid.startsWith("pwm5"))
    {
      fromAndroid.replace("pwm5", "");
      analogWrite(pwm5, fromAndroid.toInt());
      Serial.print("PWM5 VALUE: ");
      Serial.println(fromAndroid);
    }

    if(fromAndroid.startsWith("pwm6"))
    {
      fromAndroid.replace("pwm6", "");
      analogWrite(pwm6, fromAndroid.toInt());
      Serial.print("PWM6 VALUE: ");
      Serial.println(fromAndroid);
    }

    if(fromAndroid.startsWith("pwm9"))
    {
      fromAndroid.replace("pwm9", "");
      analogWrite(pwm9, fromAndroid.toInt());
   
      Serial.print("PWM9 VALUE: ");
      Serial.println(fromAndroid);
    }

    if(fromAndroid.startsWith("pwm10"))
    {
      fromAndroid.replace("pwm10", "");
      analogWrite(pwm10, fromAndroid.toInt());
      Serial.print("PWM10 VALUE: ");
      Serial.println(fromAndroid);
    }

    if(fromAndroid.startsWith("pwm11"))
    {
      fromAndroid.replace("pwm11", "");
      analogWrite(pwm11, fromAndroid.toInt());
      Serial.print("PWM11 VALUE: ");
      Serial.println(fromAndroid);
    }
 
  }//closing bracket for serial


}//closing bracket for void loop




Download Link:

1. APK = https://drive.google.com/open?id=0B3c_VPBO9Qq3dF9IdVh0X2kxWmc
2. AIA = https://drive.google.com/open?id=0B3c_VPBO9Qq3M01VUm9GYnJCbzA
3. Arduino Code = https://drive.google.com/open?id=0B3c_VPBO9Qq3TTE1bW9mUG1nLTQ

Saturday, 12 September 2015

Arduino With GPS SKM53

    GPS WITH ARDUINO MEGA 2560

Sebarang pertanyaan/pembelian/bantuan boleh dm saya dia twitter eh:


1. Where to buy this SKM53 GPS ?



Memang dari kedai beli SKM53 ni akan dapat cam ni je. Kaki dia tak ngam dengan breadboard & jumper cable. Kena beli jumper cable lain sikit dari biasa.










So, i solder this component with jumper cable.













This how to connect SKM53 to Arduino



VCC >  +5V
Gnd > Gnd
TX > digital pin 10
RX > digital pin 11

Lets go to the next steps.


Software

In this test i use TinyGPS and SoftwareSerial library. I've make a little bit changes with original code to make user easier to search their current position in Google Map.


Download Link

TinyGPS Library

Arduino Code






Arduino Code

Just copy and paste this code! Make sure you have added TinyGPS library first.
Check my older post (under ultrasonic sensor post) for tutorial adding library to Arduino IDE!


#include <SoftwareSerial.h>
#include <TinyGPS.h>   //include library

long lat, lon;

SoftwareSerial gpsSerial(10, 11);  //connect Tx and Rx pin on digital 10 and 11
TinyGPS gps;

void setup() {
  Serial.begin(9600);    //baud rate with serial monitor
  gpsSerial.begin(9600);  //GPS baud rate
}
void loop() {
  while (gpsSerial.available()) {      ///kalau GPS available, Arduino akan mula read
    if (gps.encode(gpsSerial.read()))  ///Encode signal dari GPS
    {
      gps.get_position(&lat, &lon);     ///lepas Arduino encode signal GPS, baru akan dapatkan global position
      float real_lat = (lat/1000000.000);   //divide by 1m to make user easier to get their current global postion
      float real_lon = (lon/1000000.000);
      Serial.print("google: ");
      Serial.print(real_lat);   //print latitude on Serial monitor (in two decimal places)
      Serial.print(", ");
      Serial.println(real_lon); //print longitude on serial monitor (in two decimal places)
      //////////////////////////////////////////////////////////////////////////////////////////////////
      Serial.print("Position: ");
      Serial.print("lat: ");
      Serial.print(lat);
      Serial.print(" ");
      Serial.print("lon: ");
      Serial.println(lon);
    }
  }
}




Code Explaination


#include <TinyGPS.h> 
-include TinyGPS library.

SoftwareSerial gpsSerial(10, 11);
-communication between arduino and skm53 on pin digital 10 and 11.

Serial.begin(9600);    
-baud rate with serial monitor

gpsSerial.begin(9600);
-GPS baud rate

while (gpsSerial.available())
-check if GPS available

if (gps.encode(gpsSerial.read()))
-encode GPS signal

gps.get_position(&lat, &lon);
-get the position

 float real_lat = (lat/1000000.000);
-convert latitude in two decimal places to make user easier search their curent global postion

 float real_lon = (lon/1000000.000);
-convert longitude in two decimal places to make user easier search their curent global postion

Serial.print(real_lat);
-print latitude on serial monitor

 Serial.println(real_lon);
-print longitude on serial monitor

Control RC Toys Using Your Smartphone!

       Control RC Car Using Your Smartphone!

Sebarang pertanyaan/pembelian/bantuan boleh dm saya dia twitter eh:

Assalamualaikum.. Hi guys! today i gonna tell you how to control RC car using Smartphone!




Okay, first of all we need to know the signal from smartphone transmit to your RC car. As we know, Arduino is very powerful microcontroller. Its easy to use, and widely use in engineering field. Lets check on Arduino board, we can see "TX" and  "RX" pin. We can see it on pin 0 and pin 1. So what? hahaa! Its communication pin! means we can communicate with an Arduino board! We can transmit and receive the data from Arduino. but how?

We need another device to help Arduino receive the data. We can use USB cable, Bluetooth receiver, Wifi shield and so on to help arduino receive the data/signals.




This is how HC06 looks..


In this case, we gonna use HC06 Bluetooth receiver to communicate with Arduino wirelessly. We can buy it from 



We also can get it from any hobby shop. Its price depends on quality and use. Some model just can receive or transmit data and some model can receive and transmit the data.


Now i gonna show you how to use this device with Arduino.

First we should know how to connect these four pin to Arduino
Vcc --- Connect to 5V pin on Arduino
Gnd --- Connect to Gnd pin.
RX --- Connect to TX pin!
TX --- Connect to RX pin!

Like this...




Make sure connect RX to TX !!


Okay now lets talk about how to hack the RC Car!

First of all you need to open your old RC car body.




You can see PCB with many component. Its motor driver and RF receiver. It mess you up? Relax. Just cut the wire that connect to the DC motor. Like this.






Now have 4 wires left on your old RC car. Its wire to control DC motor. Now we need another motor driver that can operate with Arduino. In my case, i use MD10 Cytron motor driver. Why do not use the old motor driver to drive the motor? it can make your circuit more complicated than you expected. This is MD10 Cytron motor driver shield that compatible with Arduino board. its easy to use!

https://docs.google.com/document/d/1acVuzZKuqC_79RzD5BRT9IQ7ug63zG0wpf-bUgQ7QHY/view




MD10 Cyrton Motor Driver


From the picture above, you can see PWM and DIR with pin D1, D2, D3, D4 and so on. Its means the digital pin that we can use for control speed of the motor(PWM) and direction of the motor rotation(DIR).

example;
in Arduino..

analogWrite(D5, 230);         //motor speed at 230
digitalWrite(DIR, HIGH);   //motor will rotate in clockwise






Its really easy to use. Just connect two DC motor wire to channel A and B. Then, connect power supply cable at +ve and -ve connector.

After that, stack them on your Arduino board. Like this.



                                                                                    MD10

Arduino Uno ----->>



Watch this video for more details step!










This is Arduino Code that i use to run my RC car. You can copy and paste. Modify it if needed :)


int pwmr = 3;      
int pwml = 5;     //use to control left motor's speed
int dirr = 2;        //control direction right DC motor
int dirl = 4;        
int nilaipwm;    //speed of DC motor


char val;
void setup() {
  Serial.begin(9600);         //baud rate
  pinMode(pwmr, OUTPUT);  //set pin to output mode
  pinMode(pwml, OUTPUT);
  pinMode(dirr, OUTPUT);
  pinMode(dirl, OUTPUT);
}


void loop() {
  if(Serial.available() > 0){         //if connection is available.
    val = Serial.read();                 //val adalah character yang COM receive
    Serial.println(val);


    if(val == '1'){                        //  '1' is data transmitted by your smartphone
      nilaipwm = 10;
    }
    
    else if(val == '2'){                  //kelajuan dc motor adalah 30
      nilaipwm = 30;
    }

    else if(val == '3'){
      nilaipwm = 50;
    }

    else if(val == '4'){
      nilaipwm = 70;
    }

    else if(val == '5'){
      nilaipwm = 90;
    }

    else if(val == '6'){
      nilaipwm = 120;
    }

    else if(val == '7'){
      nilaipwm = 160;
    }

    else if(val == '8'){
      nilaipwm = 180;
    }

    else if(val == '9'){       
      nilaipwm = 220;
    }

    else if(val == 'q'){                     //if your smartphone send 'q' , car will be move at highest speed
      nilaipwm = 255;
    }

    else if(val == 'D'){                   //  'D' untuk stop kan kereta
      nilaipwm = 0;
    }

    
   else if(val == 'F'){                               //Forward
      analogWrite(pwmr, nilaipwm);
      analogWrite(pwml, nilaipwm);
      digitalWrite(dirr, LOW);
      digitalWrite(dirl, LOW);
      delay(20);
      stopp();
    }
    
    else if(val == 'B'){                          //backward
      analogWrite(pwmr, nilaipwm);
      digitalWrite(dirr, HIGH);
      analogWrite(pwml, nilaipwm);
      digitalWrite(dirl, HIGH);
      delay(20);
      stopp();
    }

    else if(val == 'L'){                              //left
      analogWrite(pwmr, nilaipwm);
      digitalWrite(dirr, LOW);
      analogWrite(pwml, nilaipwm);
      digitalWrite(dirl, HIGH);
      delay(20);
      stopp();  
    }

    else if(val == 'R'){                             //right
      analogWrite(pwmr, nilaipwm);
      digitalWrite(dirr, HIGH);
      analogWrite(pwml, nilaipwm);
      digitalWrite(dirl, LOW);
      delay(20);
      stopp();  
    }
  }
}

void stopp(void){                     //void to stop the car
  analogWrite(pwmr, 0);
  analogWrite(pwml, 0);
  
  }







Explaination

int pwmr = 3;
tell arduino that i use pin3 to control speed DC motor on right side.
int dirl = 4;
tell arduino, i use pin4 to control direction of left DC motor.

Serial.begin(9600);
speed of communiction data transmit and receive

Serial.available() > 0
If port receive any signal/data more than 0

val = Serial.read();
"val" is variable that hold data transmitted by your smartphone in string

else if(val == 5)
if serial receive data '5' , arduino will tell motor driver to speed up motor speed at 90

else if(val == 'F')
if serial read is equal to 'F' , car will move forward.





Download Smartphone App & Arduino Code

Arduino Code
https://drive.google.com/file/d/0B3c_VPBO9Qq3SkNLNndKUEU0WFU/view?usp=sharing

Smartphone App
https://play.google.com/store/apps/details?id=braulio.calle.bluetoothRCcontroller&hl=en