Tuesday, February 16, 2016

Playing Ultrasonic sensor with Arduino


Playing Ultrasonic sensor with Arduino


1. The Connection


1424201420-2wire_bb.png




2. The script

#define echopin 3
#define TRIGPIN 2

void setup()
{
  Serial.begin (9600);
  pinMode (echopin,INPUT);
  pinMode (TRIGPIN, OUTPUT);
}
void loop()

{ digitalWrite(TRIGPIN,LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN,HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN,LOW);

float distance = pulseIn(echopin,HIGH);
distance = distance/46;
Serial.print (distance);
Serial.println (" cm");
if (distance < 10)
{
Serial.print("AWAS HATI-HATI");
Serial.println (" Jarak kurang dari 10 cm");
}
else if
 (distance < 20)
{
Serial.print("Waspada");
Serial.println (" Jarak kurang dari 20 cm");
}
else
{
  Serial.println ("Jarak Masih Aman");

}
delay (200);
}

playing with Gyro sensor GY-291 and Arduino



1. connection.





2. The Script

/* Include the standard wire library */
#include

/* Alternate I2C address of the module */
#define I2C_Add 0x53

/* ADXL345 register addresses */
#define POWER_CTL 0x2D
#define DATA_FORMAT 0x31
#define X_Axis 0x32
#define Y_Axis 0x34
#define Z_Axis 0x36

/* Accelerometer range modes */
#define RANGE_2g 0
#define RANGE_4g 1
#define RANGE_8g 2
#define RANGE_16g 3

void setup()
{
  /* Initialise the I2C bus */
  Wire.begin();  
  
  /* Initialise the serial interface */
  Serial.begin(9600);
  
  /* Initialise the ADXL345 */  
  Init_ADXL345(RANGE_2g);
}

/* Main program */
void loop()
{
  /* Continually read and output all 3 axis to the serial port */
  Serial.print("X: ");
  Serial.print(Read_Axis(X_Axis));
  
  Serial.print(" Y: ");
  Serial.print(Read_Axis(Y_Axis));
  
  Serial.print(" Z: ");
  Serial.println(Read_Axis(Z_Axis));
}

/* Read one of the 3 axis via the I2C interface */
int Read_Axis(byte axis)
{
  int Data;
   
  Wire.beginTransmission(I2C_Add); 
  Wire.write(axis); 
  Wire.endTransmission(); 
  
  Wire.beginTransmission(I2C_Add);
  Wire.requestFrom(I2C_Add, 2);
  
  /* If data is available then read it (2 bytes) */
  if(Wire.available())     
  { 
    Data = (int)Wire.read();
    Data = Data  | (Wire.read() << 8);
  }else
  {
    Data = 0;
  }
    
  Wire.endTransmission();  
  return Data;
}


/* Initialise the ADXL345 */
void Init_ADXL345(byte range)
{
  Wire.beginTransmission(I2C_Add);
  
  /* Set the sensitivity of the module */
  Wire.write(DATA_FORMAT); 
  Wire.write(range); 
  Wire.endTransmission(); 
  
  /* Put the module into measurement mode to start taking measurements */
  Wire.beginTransmission(I2C_Add);
  Wire.write(POWER_CTL); 
  Wire.write(0x08); 
  
  Wire.endTransmission(); 
}


Friday, February 5, 2016

Controlling arduino using serial openwrt

Controlling arduino using serial openwrt




1. Create sketch

// include the library code:
#include

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
}

void loop() {
  // when characters arrive over the serial port...
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0) {
      // display each character to the LCD
      lcd.write(Serial.read());
    }
  }
}

2. Upload the sketch
3. For testing the arduino ,open serial monitor, and type something.


4. Install modem driver on openwrt

root@OpenWrt:~# opkg install kmod-usb-serial kmod-usb-serial-ftdi

5. Attach Arduino to OpenWRT, and check on dmesg

[ 5182.260000] usb 1-1: new full-speed USB device number 3 using ehci-platform
[ 5182.430000] cdc_acm 1-1:1.0: ttyACM0: USB ACM device

6. Install coreutils-stty

root@OpenWrt:~# opkg install coreutils-stty

7. Add  this to /etc/rc.local before start
stty -F /dev/ttyACM0 4:0:18b2:0:0:0:0:0:1:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:
stty -F /dev/ttyACM0 9600 raw
=> Reboot router

8. Test from openwrt,

- open ssh to IP of openwrt
- type echo -en "Selamat Siang" > /dev/ttyACM0   than press enter.

9. Result on LCD screen.