Monday, May 24, 2010

Aurduino Oscilloscope

having a oscilloscope has been a dream for me since i started leaning electronics, because i can't afford buying an Oscilloscope( i never buy second hand electrical equipment ). Therefore i decided to solve my problem by myself, However i kinda failed.
The Arduino Oscilloscope has many problems such as its range ( ~ 1Khz :) ) and its scale ( there is no scale on the screen). the input also must be between 0 to 5 volt which is not a problem ( it can be increased by voltage dividers or using transistor). After all there are the Code for arduino and visual basic .... have some fun

**********************************Aurduino**************************

int incomingByte = 0;
const int analogInPin = 0;

int sensorValue = 0; // value read from the pot

void setup()
{
Serial.begin(115200);
pinMode(13,OUTPUT);
}


void loop()
{
sensorValue = analogRead(analogInPin);


Serial.println(sensorValue);


incomingByte=0;
delay(1);

}

NOTE: if u erase the delay, the computer will be left behind the data. Therefore the screen will be 20 sec behind what is actually happen .

*************************************Visual basic**************************


********* 3 text boxes and and a bottom are needed *****************
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

sp1.PortName = TextBox1.Text

sp1.Open()
Dim myPen As Pen = New Pen(Color.GreenYellow, 5)

sp1.Write(1)
Dim i As Integer
Dim u As Decimal
Dim x As Decimal
Dim z As Integer
Dim x1 As Decimal
Dim voltager As Integer
Dim timerange As Integer
Dim timerangex As Integer

voltager = Val(TextBox2.Text)
timerange = Val(TextBox3.Text)

Dim a As String

Dim n As Single
n = 0
Me.CreateGraphics.Clear(Color.Black)
Do
n = n + 1
If n = (800 / timerange) Then
n = 0
Me.CreateGraphics.Clear(Color.Black)
End If

a = sp1.ReadLine

z = Val(a)
x = 5 / 1024 * z
u = (5 * voltager)

x = x * voltager
x = u - x


Me.CreateGraphics.DrawLine(myPen, timerange * n, x1 - 1, timerange * n, x)

x1 = x



Loop




sp1.Close()

End Sub

End Class
*******************************************************

Friday, May 14, 2010

Infrared Radar

this is a infrared radar which has 1m rang. the radar is consisted of a servo, Sharp IR sensor and audrino board. the reason i used the audrino board is that i wanted to do a fast work and not spend time on making a PCB or soldering.However learning references of the language used by Aurdino compiler took time. i have tried many time in past to measure the distance by using IR transistor and Ir transmitter LED, but the result wasn't really good. the longest distance that i could measure was 10 cm which was effected by light of the sun.Three very important factors have caused my experimences to fail: 1-the angel between the transmitter and receiver 2- the type of transmitter and reviver 3- time ( it takes so much time to find problems and fix the circuit which you has designed ).. After all, i decided to use Sharp IR sensor.
As I've said, the language used for programming the microcontroller is C which was complied by audrino compiler
here is the cod:


-------------------------------------------------------------------------------------
"#include
int incomingByte = 0;
const int analogInPin = 0;
const int analogOutPin = 9;
int sensorValue = 0; // value read from the pot
int outputValue = 0;
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position

void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}


void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{

sensorValue = analogRead(analogInPin);
if (sensorValue>300){
digitalWrite(13,HIGH);
}
if (sensorValue<300){
digitalWrite(13,LOW);
}




Serial.print(sensorValue);
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.print(",");
Serial.println(pos);
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();

}
while (incomingByte != 49) {
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();

}
}


incomingByte=0;
delay(1); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{

sensorValue = analogRead(analogInPin);

if (sensorValue>300){
digitalWrite(13,HIGH);
}
if (sensorValue<300){
digitalWrite(13,LOW);
}

Serial.print(sensorValue);
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.print(",");
Serial.println(pos);
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();

}
while (incomingByte != 49) {
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();

}
}

incomingByte=0;
delay(1); // waits 15ms for the servo to reach the position
}
}
-------------------------------------------------------------------------------------
the out put is a line consisted of angle and the value of sensor => value,angle
exp: 425,180
the microcontroller also needs to receive the character "1" ( 65 in ASCII) to move to next angle ( it must be send after finishing of whole process by computer to draw the graph)
-------------------------------------------------------------------------------------
VISUAL BASIC 2008
the windows based program to show the graph was written on visual basic
note: two textboxs, a bottom , and serialport control is needed


here is the code for visual basic 2008:
-------------------------------------------------------------------------------------
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bit As Bitmap = New Bitmap(Me.Width, Me.Height)
Dim g As Graphics = Graphics.FromImage(bit)
Dim myPen As Pen = New Pen(Color.GreenYellow, 5)
Dim myPen2 As Pen = New Pen(Color.Yellow, 3)
Dim myPen3 As Pen = New Pen(Color.Blue, 3)
sp1.Open()
sp1.Write(1)
Dim i As Integer
Dim f As Decimal
Dim j As Decimal
Dim x As Integer
Dim z As Integer
Dim w As Decimal

Dim a As String
Dim b As Integer
Dim k As String
Dim c As Integer
Dim u As String
Dim y As String
Me.CreateGraphics.Clear(Color.Black)
Me.CreateGraphics.DrawRectangle(myPen2, 350, 350, 10, 10)
Me.CreateGraphics.DrawRectangle(myPen2, 0, 0, 190, 10)
Dim n As Single
n = 0
Do
n = n + 1
If n = 180 Then
n = 0
Me.CreateGraphics.DrawRectangle(myPen2, 0, 0, 190, 10)
Me.CreateGraphics.Clear(Color.Black)
End If
Me.CreateGraphics.FillRectangle(Brushes.Red, n, 0, 10, 10)
a = sp1.ReadLine
c = 0
u = ""
y = ""
For b = 1 To Len(a)
k = Mid(a, b, 1)
If k = "," Then
c = 1
End If
If c = 1 Then
If Not k = "," Then
u = u + k
End If
End If
If c = 0 Then
y = y + k
End If
j = Val(u) / 180 * 3.14
w = j
f = Val(y) / 2
f = 350 - f
x = 350 - f * Math.Cos(j)
z = 350 - f * Math.Sin(w)




Next
TextBox1.Text = TextBox1.Text + y + u
TextBox2.Text = TextBox2.Text + CStr(x) + " " + CStr(w) + " " + CStr(z) + "..."
Me.CreateGraphics.DrawLine(myPen, x - 2, z - 2, x, z)

Me.CreateGraphics.DrawLine(myPen3, 350, 350, x, z)
sp1.Write(1)
Loop
Me.CreateGraphics.DrawRectangle(myPen2, 350, 350, 10, 10)
Me.CreateGraphics.FillRectangle(Brushes.Yellow, 350, 350, 10, 10)

sp1.Close()

End Sub


Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub
End Class
---------------------------------------------------------------------------------
to find X and Y for drawing the sin and cos has been used.IT's a simple math concept
x=cos(angel)*length
y=sin(angle)*length





------------------------------------Circuit-----------------------------------------


the board is so easy :))))))))))) just connect the data output of IR receiver to analog port #0 of aurdio . the servo also needs to be connected to the pin number 9
CHECK THE VIDEO CLIP OF THIS PROJECT

*****************ALL PROGRAMS HAS BEEN WRITTEN BY DANIAL MOHAMMADI************