tiistai 26. helmikuuta 2013

Powering arduino

I decided to write shortly about powering arduino UNO. There are few ways to power it. I don't know if these voltage rates are the same for every board but these goes for UNO.

1. From usb cable with computer or 230v usb adapter
2. Any kind of battery with 7-12 volts, example AA batteries, 9v battery, car battery, rc-car battery etc.

More detailed info can be found on arduino.cc

Connection, either + (red wire) to VIM port, or GND (black wire) to GND of course.

I am using this for my UNO: (ordered from www.dx.com)


maanantai 25. helmikuuta 2013

Reading from serial port part 2

This is the same method as described 2 posts below. I am just reading PING from terminal. My program is the same as in the post below. 




keskiviikko 20. helmikuuta 2013

hc-sr04 ultrasonic sensor, with 4 legs + servo

I got new  hc-sr04 sensor with 4 legs. GND, ECHO, TRIG, VCC.

Connection setup: GND to GND of course, VCC goes +5V, echo to pin 13, trig to pin 12



This code prints distance to serial monitor:


#define trigPin 12
#define echoPin 13

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

Then i added a servo which moves if its between the wanted range:



Connection: servo to analog 0, PING to 12 and 13 as described in the actual code:


#define trigPin 12
#define echoPin 13
#include <Servo.h>
Servo servoMain; // Define our Servo

void setup() {
  Serial.begin (115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  servoMain.attach(A0); // servo on analog pin 0
}

void loop() {
servoMain.write(90);  // Turn Servo back to center position (90 degrees)

  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
 
    Serial.print(distance);
    Serial.println(" cm");

  if (distance >=10 && distance <= 30){
     servoMain.write(135); // Turn Servo Right to 135 degrees
     delay(1000);          // Wait 1 second
     servoMain.write(25);  // Turn Servo Left to 45 degrees
  }
  else {
    servoMain.write(90);  // Turn Servo back to center position (90 degrees)
  }
  delay(500);
}




Then i wanted to test with the new battery case i got. I soldered jumper wires to the case so its easier to connect power to arduino.



 Just put plus (red wire) to VIM port and black one to GND = ground.  I am using arduino UNO which works with these voltages:
Input Voltage (recommended)7-12V
Input Voltage (limits)6-20V



perjantai 15. helmikuuta 2013

Reading and writing serial ports with python

Today we had to find information how to read information from serialport with computer. First we write a simple program for arduino which prints something for serial monitor:


void setup()
{
  Serial.begin(115200);
  Serial.println("Foo bar");
}

void loop()
{
  Serial.println("Moi");
  delay(500); //ms
}

This prints to arduino serial monitor, first "Foo bar" only once, and then "Moi" always after 0,5 seconds.


Then we want to read that from usb port. You must have permissions to read from serial port. Example sudo chmod 777 /dev/ttyACM0 (see the right port from arduino ide.

Create a new file in linux: cat > read.py
open it with text-editor, i used nano: nano read.py

Then we write a simple python program to read that from usb port (copy this code and also remember to indent the code for if and while parts.):


import serial, sys

ser = serial.Serial("/dev/ttyACM0", 115200)

if (ser):
print("Serial port " + ser.portstr+ " opened.")

while True:
sys.stdout.write(ser.read(1) )
sys.stdout.flush()

Then open the file in terminal: python read.py
And we can see it prints that program written for arduino.


And if you want to write something, just add this line
ser.write ('5')

keskiviikko 6. helmikuuta 2013

Arduino Flame Detection Sensor Module

Our home task was to find out what the sensor does. I got flame detection sensor module which detects infrared rays. We connected one led to the sensor, so it would set on if the sensor detects infrared. We had a remote control which of course works with infrared. Always when we pushed a button the led turned on. It worked pretty well.






The code:

/*SENSOR PINOUT:

PIN 1: Analogue out
PIN 2: Ground
PIN 3: +5V
PIN 4: Digital out

You may copy, alter and reuse this code in any way you like but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.

Modified by Niki Ahlskog & Anders Borgström */


/* Select the input pin for the flame detectors analogue output. */
#define FLAME_DETECT_ANA A0

/* Select the input pin for the flame detectors digital output. */
#define FLAME_DETC_DIO 2
int led = 8;


/* Initialise serial and DIO */
void setup()
{
/* Setup the serial port for displaying the status of the sensor */
Serial.begin(9600);
pinMode(led, OUTPUT);
/* Configure the DIO pin the sensors digital output will be connected to */
pinMode(FLAME_DETC_DIO, INPUT);
}


/* Main program loop */
void loop()
{
/* Read the sensors analogue output and send it to the serial port */
Serial.print("Sensor Value: ");
Serial.print(analogRead(FLAME_DETECT_ANA));

/* Read the status of the sensors digital output and if it is high
then send an alert to the UART */
if (digitalRead(FLAME_DETC_DIO))
{
Serial.println(" FLAME DETECTED!");
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);

}else
{
Serial.println();
digitalWrite(led, LOW);

}

}

The sensor: http://dx.com/
Original code: http://www.hobbycomponents.com/



Arduino servo motor

It moves! This time i got black servo motor version s3003. I just wanted to see if I can control it somehow and yes, I can. "Radio Control Servos are great pieces of kit. They are essential in pretty much every robot build, from controlling arms and legs to driving wheels and tracks. Servos normally rotate up 180 degrees with the 90 degree mid-point being the center position, and can be positioned at any point in-between. "






#include <Servo.h>
Servo servoMain; // Define our Servo

void setup()
{
   servoMain.attach(A0); // servo on analog pin 0
}

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

Very simple code how to control servo.


perjantai 1. helmikuuta 2013

Arduino Piezo Sensor - LDT1-028K

This time i got Piezo sensor LDT1-028K which is some kind of paper slip which can detect touching or  twisting and as in description is said, vibration and impact. My setup is the slip and led. If the slip is touched the led should set on and if it's not touched the led should be off. The only problem is that the slip is throwing random numbers and I can't see any logic there... But anyway it's working somehow.


---------------------------------------------------------------------------------------------------------
Description: The LDT1-028K is a multi-purpose, piezoelectric sensor for detecting physical phenomena such as vibration or impact. The piezo film element is laminate to a sheet of polyester (Mylar), and produces a useable electrical signal output when forces are applied to the sensing area. The dual wire lead attached to the sensor allows a circuit or monitoring device to process the signal.


Features:

• Piezoelectric polymer

• Multi-purpose

• Vibration sensing

• Impact sensing

• Laminated

• Dual wire lead attached

• Minimum impedance: 1 MΩ

• Preferred impedance: 10 MΩ and higher

• Output voltage: 10mV-100V depending on force and circuit impedance

• Storage temperature: -40°C to +70°C [-40°F to 160°F]

• Operating temperature: 0°C to +70°C [32°F to 160°F]

---------------------------------------------------------------------------------------------------------


The actual code goes like this:

int sensorPin = A0; // sensor must be in analog input
int ledPin = 7; // led is connected to port 7
int sensorValue = 0; // here is defined the value for sensor

void setup() {
pinMode(ledPin, OUTPUT); // led is in output which means it sends information to outside world
Serial.begin(115200); // set reading to the fastest speed possible
}

void loop() {

sensorValue = analogRead(sensorPin); // here we read the numbers from sensor

  // from device monitor we can see values are between 0 - 1023

if (sensorValue < 500 && sensorValue > 50) // if value is smaller than 500 but bigger than 50
{
   digitalWrite(ledPin, HIGH); // led will be on
}

else
{
  digitalWrite(ledPin, LOW); // if the value is something else led is off
}


// sensorValue = 0; // not sure if the sensor value should be resetted somehow

// printing to the serial monitor
  Serial.print(sensorValue);
  Serial.print("\t");
  Serial.print(sensorValue);
  Serial.println();
}



Links to the sensor:

http://www.meas-spec.com/product/t_product.aspx?id=5435
http://www.seeedstudio.com/wiki/index.php?title=Piezo_Sensor_-_LDT1-028K_Lead_Attachments

Like this the serial monitor shows random numbers and there is no logic I think. Normally this sensor is used to measure jolts i heard. Anyway I got it to work somehow.