This page is a random collection of notes addressed to myself. Nothing here is intended as a guide per say, however i have posted them hoping that it may help someone.
Blog about Arduino
http://arduino.cc/
Everything in this blog can be copied or edited GNU General Public License (version 2 or newer) http://www.gnu.org/licenses/gpl.html
Arduino course is now finished and I hope that I will write here again some day. Here are some projects our course did.
First on is push-up counter. It uses ultrasonic sensor to detect how close the doer is. Then it counts the amount of push-ups to the digital screen. It can be restarted with button. Simple, but works. No idea if someone needs this or not ?
Second one is some kind of watch tower or big brother tower. It turns 180 degrees and scans the area. If it detects movement, it stops and turns a led light on. The original idea was to make kitchen lights turn on and off automatically.
Third one was interesting. A robot hand. It is made of plastic bottle and cardboard. Every finger is connected to one servo motor with a thread and when it moves the hand "grabs". The hand-guy first tried to make a 3D print of hand but it failed because software incompatibilities.
Last one was a flashlight button tester. It can be set to push forever the flashlight button, it tests if the light turns on and if not it calculates it as error. It didn't save any log file yet but it was under work. Very useful tool for flashlight producers i'd say.
And my robot hat, teacher liked it. I got 4,5/5 points. That was very nice.
As my final project I got an idea about a hat which can talk to the user and guide example a blind guy to the right way and not to crash anything. Or one could solve a maze in complete darkness because the ultrasonic sensor. I first tried to make Arduino say something but it didn't succeed. Then I eased my project and thought maybe same kind of noise what car rear-view cameras make. I found Arduino Tone library. With that I managed to make Arduino Beep! Then I found a broken headset. I ripped the speaker elements out of it and soldered two wires to it. Then I connected the ultrasonic sensor which I got earlier hc-sr04 to Arduino, tested if it works and can measure the distance. I coded it to check if the distance is between three ranges. If the range is shorter than 30 centimeters it will beep very fast and warn the user that you are going to crash. If the distance is between 31 and 60 centimeters it will beep normal speed and it tells that it is safe to turn. If the distance is between 61 and 90 it beeps very slow to warn that you are approaching an obstacle. If the distance is bigger than 90 it stays silent. I think this was very fun project and of course interesting. Thanks to Tero Karvinen i'm now very excited about the Arduino and its possibilities. And then about the project.
I first started to code the project by one piece at a time. First the ultrasonic sensor, then the beep sound and then connected those together. Here i'm currently in test environment.
Next I had to hot glue all the parts to an old peaked cap and solder few wires together.
Then I tested if the parts still work without computer test environment and it didn't. I had to change the speaker connection a little bit and then it worked. Here is the final ultrasonic hat. The battery plus wire was too short but it is just long enough to work.
Testing phase for distance and beep to function correctly together.
And here it is. The final version of my prototype hat for the blind people or drunk people or what ever.
And how I coded it ? Here is the whole code.
Connection like in ultrasonic sensor post and tone post. hc-sr04 vcc to +5v, gnd to gnd, trig to pin 12, echo to pin 13. Speaker element goes to ground and digital pin 8.
//define the pins for ultrasonic sensor here
#define trigPin 12
#define echoPin 13
//setup trig is output and echo is input for the sound. Read serial fastest possible speed
void setup() {
Serial.begin (115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
//below methods for beep speed
void beepFast(){
// play a note on pin 8 for 200 ms:
tone(8, 440, 200);
delay(50);
// turn off tone function for pin 8:
noTone(8);
delay(300);
}
//no beep here
void beepNo(){
noTone(8);
delay(300);
}
//send a pulse and calculate how long it took to travel
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
//then check the distance and beep if it is in the range
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else if (distance >=0 && distance <=30){
beepFast();
Serial.print(distance);
Serial.println(" cm");
}
else if (distance >=31 && distance <=60) {
Serial.print(distance);
Serial.println(" cm");
beepMedium();
}
else if (distance >=61 && distance <= 90){
Serial.print(distance);
Serial.println(" cm");
beepSlow();
}
else {
beepNo();
Serial.print(distance);
Serial.println(" cm");
}
}
And the guy behind this blog and random hat project...
Testing phase. She managed to walk to the door from kitchen and back. Succeed.
The project was fun to do and i'm proud that I managed to complete it. One thing more to add my portfolio.
Because I got the dc motor work with Arduino I thought maybe I should build a car. The idea was good but the actual car was bad. DO NOT build a car with weak dc motor. It runs very slow because the DC motors are so weak and the controlling of motors is also bad. Anyway here is a picture and a video to demonstrate it. I'm not even using Arduino because I just wanted to know if the motors are good for this kind of project. And the answer is definitely no.
As you can see it didn't even have enough power to run on carpet...
I discovered that Arduino can play simple beep sounds without anything special. I ripped the speaker element from old headphones, soldered two wires to it and connected to arduino GND and digital 8 through 100ohm resistor. It played continuous beep sound... was pretty fun.
Found the code from here: http://arduino.cc/en/Tutorial/Tone4
bEEp! bEEp! bEEp! bEEp! bEEp! bEEp!
void setup() {
}
void loop() {
// turn off tone function for pin 11:
noTone(11);
// play a note on pin 6 for 200 ms:
tone(8, 440, 200);
delay(200);
// turn off tone function for pin 6:
noTone(8);
delay(300);
To control small DC motor you will need:
motor, max 5v
PN2222 Transistor
1N4001 diode
270 Ω Resistor (red,
purple, brown stripes)
Connection scheme:
Add this code to arduino. You can control the speed of motor by sending values between 0 - 255 from serial monitor. However i noticed my DC motor doesnt work if the speed isn't between 150 - 255.
int motorPin = 3;
void setup()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
while (! Serial);
Serial.println("Speed 0 to 255");
}
void loop()
{
if (Serial.available())
{
int speed = Serial.parseInt();
if (speed >= 0 && speed <= 255)
{
analogWrite(motorPin, speed);
}
}
}
Project found from: http://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors/overview
I started to do the second project from Arduino book because I have limited parts to use and it seemed really interesting. So I started by gluing two servos together and disassembling two rc helicopters.
This is the first version of the Arduino insect:
The legs were really bad and it didn't move an inch. Then i followed the book and got two wire hangers. The legs were far more better and the insect even moved very slow. This is the second version or third version because I added the PING sensor to be the eyes. It doesn't work yet but i'm working on it.
Version 1 video:
And the actual code how it moves. The idea is to move servos at different time.
-------------------------------------------------------------------------
The insect can move by calling the method
-------------------------------------------------------------------------
#include <Servo.h>
Servo frontServo;
Servo rearServo;
int centerPos = 90;
int frontRightUp = 72;
int frontLeftUp = 108;
int backRightForward = 75;
int backLeftForward = 105;
-------------------------------------------------------------------------
Move turn left
-------------------------------------------------------------------------
#include <Servo.h>
Servo frontServo;
Servo rearServo;
int centerTurnPos = 81;
int frontTurnRightUp = 63;
int frontTurnLeftUp = 117;
int backTurnRightForward = 66;
int backTurnLeftForward = 96;
/*Servo motors global variables */
int centerPos = 90;
int frontRightUp = 72;
int frontLeftUp = 108;
int backRightForward = 75;
int backLeftForward = 105;
int walkSpeed = 150;
int centerTurnPos = 81;
int frontTurnRightUp = 63;
int frontTurnLeftUp = 117;
int backTurnRightForward = 66;
int backTurnLeftForward = 96;
This is the current version of insect with better legs. Now it can move forward a little bit better. It also has a place for PING sensor but I didn't add it yet.
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)
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:
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:
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')
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);
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. "
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
}
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]
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();
}
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.
This time i got Memsic accelerometer. The task was to find out what this sensor is and what it does.
The first thing what I did was that I opened arduino.cc and searched that sensor. There was a clear picture how to connect the sensor and in the development environment we found under sensors the basic code for this sensor. Link to the sensor: http://arduino.cc/en/Tutorial/Memsic2125?from=Tutorial.AccelerometerMemsic2125
Then we just opened device monitor to see which axis where which and then added two leds which start to blink slower when the angle is growing. Picture of the setup:
The actual code, we added if-else statement that if the angle is growing the led should blink slower. Few videos here about the testing:
The code:
// these constants won't change:
const int xPin = 2;// X output of the accelerometer
const int yPin = 3;// Y output of the accelerometer
void setup() {
// initialize serial communications:
Serial.begin(9600);
// initialize the pins connected to the accelerometer
// as inputs:
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
// variables to read the pulse widths:
int pulseX, pulseY;
// variables to contain the resulting accelerations
int accelerationX, accelerationY;
// read pulse from x- and y-axes:
pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin,HIGH);
// convert the pulse width into acceleration
// accelerationX and accelerationY are in milli-g's:
// earth's gravity is 1000 milli-g's, or 1g.
accelerationX = ((pulseX / 10) - 500) * 8;
accelerationY = ((pulseY / 10) - 500) * 8;
Me and Anders got a PING ultrasonic sensor. Our task was to find out what it does and how. Here is the sensor:
It sends ultrasonic sound from the other tube and then the other one is microphone which caches the sound. Then the actual code calculates how long it took for the sound to travel to the obstacle and back. Here is some basic code which sets the led in socket 13 on IF the distance is bigger than 10. If it's smaller than ten the led will turn off.
Connect PING signal to pin 7, GND to GND and power to +5v
#define fP 7 //Pin the ping sensor is connected to
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
if (ping() > 10) // set distance
{
digitalWrite(13, HIGH); // set the LED on
}
else
{
digitalWrite(13, LOW); // set the LED off
}
}
int ping() //Get CM to obstacle in front of the sensor
{
long duration;
pinMode(fP, OUTPUT);
digitalWrite(fP, LOW);
delayMicroseconds(2);
digitalWrite(fP, HIGH);
delayMicroseconds(15);
digitalWrite(fP, LOW);
delayMicroseconds(20);
pinMode(fP, INPUT);
duration = pulseIn(fP, HIGH);
return duration / 29.0 / 2.0;
}
Then we just added more leds and added distances when the led's should be active:
I forgot to shoot my own video, but my friend took one and here is how it works.
//#define fP 7 //Pin the ping sensor is connected to
const int pingPin = 7; //set ping to slot 7
void setup()
{
pinMode(4, OUTPUT); //
pinMode(5, OUTPUT); //set led to slot 4,5,6
pinMode(6, OUTPUT); //
}
void loop()
{
if (ping() > 2) //if ping distance is bigger than #
{
digitalWrite(4, HIGH); //set the LED on
}
else if(ping() > 6)
{
digitalWrite(5, HIGH);
}
else if(ping() > 10)
{
digitalWrite(6, HIGH);
}
else
{
digitalWrite(4, LOW); //
digitalWrite(5, LOW); //set the LED off
digitalWrite(6, LOW); //
}
}
int ping() //Get CM to obstacle in front of the sensor
{
long duration;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(15);
digitalWrite(pingPin, LOW);
delayMicroseconds(20);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
return duration / 29.0 / 2.0;
}
First task was to burn xubuntu live cd. I burned other cd's as well.
First lesson of prototype course was very impressive, all other courses are boring or I feel somehow stupid doing other courses. This was the first course this year when I felt I'm in the right place. Maybe later I will attend other Tero's courses if I have time.
The idea of the course is to build your own prototype and show it to others. Prototype is some kind of demoversion. Here are the "brains" called arduino in the picture.
First program or machine:
I installed Lubuntu in dualboot and then opened command prompt:
sudo apt-get update (here i updated the software database)
sudo apt-get install arduino (this code installs the arduino environment)
newgrp dialout (adds dialout group which is needed in arduino)
arduino (starts the actual programming thing)
At first i downloaded Arduino "Hello World!" program which blinks led light in socket 13 with 1 second delay. It worked and so I was sure the developing environment worked. Then I installed three leds on the testing board and programmed them to blink in order, first led blinks 3 times, second 2 times and the last one of course once. It worked very well ! Here is a video how the system works:
Last task was to invent a system with uses some sensor. I couldn't think anything so i just developed basic alarm system. We could use a ultrasonic sensor or infrared if there is a movement in the house or some place and then send email or pictures with webcam to internet to see what is currently happening in the house.