Coming back to life

Well, tekstop is coming back to life after a period of 8 months! I promise that this time I am going to be more responsive to technology and have more posts than before (shouldn’t be too hard 😉 since I only had 2 real posts). But seriously, things are going to get a lot more interesting.

One of things that has changed is the tag of my blog has gone from all things mechatronics to “all thing tech!”. This will help the blog to grow faster, since I am more frequently surrounded by software rather than hardware. I still hope to keep the mechatronics in there, but will mix it up with a lot more about software.

So with this out-of-the-way, stay tuned for quick updates 🙂

Sharp IR sensors with arduino

One of favorite sensors of all time are the Sharp IR proximity sensors. These sensors are quite accurate and can be used for a variety of tasks. These are single package sensors that basically work on the principle of reflection. They output analog voltage corresponding to the distance measured (from 1 – 3.2V between distances of 4cm to 80cm). One of the main reasons why I love these sensors is that they are continuous and do not require a ping from the micro controllers to send out a pulse (like most ultrasonic sensors do!).

They are available in a number of configurations, each offering different distances. The page at acroname has a wonderful guide on these sensors.

The sensor that I am using for the current experiment is GP2D12. In this experiment, I will basically be reading the value from the sensor and based upon the readings changing the brightness and colors of the Tricolor Led.

The breadboard circuit is as shown below:

The nifty figure is created using a free software Fritzing (check it out!). This software allows you to assemble your circuit in Breadboard while simultaneously creating a schematic. It also creates a PCB (with a cool auto routing feature). Though it doesn’t seem to have all the bells and whistles that Eagle gives, it is really great for simpler circuits. Below, are schematic from Fritzing as well as Eagle. In my opinion, the Eagle schematic looks better or rather more circuitish!

Schematic with Fritzing

 

Schematic with Eagle

The actual setup is shown below:

Before using any sensor, it is important to calibrate the sensor. Sharp sensors are almost linear in their major range of operation. However, since they are reflection sensors, a lot depends on what you are trying to detect. I typically calibrate them over a subset of their entire range. Here, I have calibrated them over 20 – 50 cm. The experiment changes the brightness of the RED led from 20-30 cm, GREEN led from 30 – 40 cm and BLUE led above 40 cm.

The source code (not the best) is written in C/C++ instead of Processing. I find writing C++ easier, as it allows me to use a full fledged IDE (XCode in this case) and gives access to other libraries that are only in C/C++. You can configure almost any IDE to directly compile and upload code to the arduino. For Xcode, there is an excellent blog that covers all the steps. For Eclipse (free IDE across platforms), there are plenty of tutorials online.

#include "WProgram.h"
#include "HardwareSerial.h"
#include "SharpSensor.h"
void ledIntensity(int pin,int val)
{
if (val > 255)
{
val = 255;
}
else if (val <0)
{
val =0;
}

Serial.println(pin);
Serial.println(val);
analogWrite(pin,val);
}

void turnoff(int pin)
{
analogWrite(pin,255);
}

int analogPin = 5;
float value =0.0;
int green = 3;
int blue = 5;
int red = 6;
SharpSensor obj;
extern "C" void __cxa_pure_virtual() { while (1); }

void setup()
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(green, OUTPUT);
pinMode(blue,OUTPUT);
pinMode(red,OUTPUT);
}

void loop()
{

value = obj.readInCM(analogPin);
if(value > 40.00 )
{
turnoff(red);
turnoff(green);
ledIntensity(blue,(int)(255-(value-40)*20));

}
else if (value > 30.00 && value < 40.00)
{
turnoff(red);
turnoff(blue);
ledIntensity(green,(int)(255-(value-30)*20));

}
else
{
turnoff(green);
turnoff(blue);
ledIntensity(red,(int)(255-(value-20)*20));

}

delay(100);
}

int main(void)
{
init();

setup();

for (;;)
loop();

return 0;
}

Update:
I created a small Class/ header to wrap the common functionalities of the sensor. You can download this at http://www.mediafire.com/?ddt3uu47axm2c

What this blog is going to be all about!

Well, this is my first blog and it’s just an introduction about my blog and nature of the content I will be posting.

From the name,  it is very easy to guess that all my posts are going to be tech related and no, I don’t mean reviews of stuff but rather my experiments with them. They are going to be tutorial in nature about various mechatronix projects I attempt during my free time.

Starting from the simplest things to as advanced I can get! If you find any post helpful just leave a comment!

The areas I will be touching upon are: robotics, arduino, micro controllers, IDEs etc… I guess you get the drift!

Blinking colors with the arduino

The Arduino is an open source project that provides an easy to use micro controller development board running various ATMEL Avrs.

Like all first micro controller projects, this one too is also about blinking a LED (‘The Hello World of embedded systems’). The only difference here is that I tried to step it up to blink different colors using a RGB tricolor LED. Again, nothing original, their is a good tutorial regarding the same on Wiring. The tutorial there uses the same LED as provided by Sparkfun. For me, personally, Sparkfun, along with Pololu, adafruit and acroname are the best places to look for electronics and other related items.

This tutorial just uses the Tricolor LED available at Radioshack. The basic difference between this LED and the other one is that this is just reversed. To drive the LED, it must be powered through the common terminal and the other pins must be held low to complete the circuit.

The complete schematic of the circuit is shown below:

The schematic sketch is created in Eagle (free software) and is amazing, especially after adding the Sparkfun components library.

The basic Arduino board is used and Pins 11, 12 and 13 are wired to the Red, Blue and Green terminals for the LED. Be sure to add resistors to limit the current (prevent overheating etc) depending upon the forward voltage for the LED. The LED above had Red at a lower voltage and hence a higher resistance in series.

The image below shows the connections:

Once the connections are set up, it’s just about writing some code! The arduino platform provides a simple IDE and the language is basically a derivation of Processing. It is straightforward and the arduino homepage has great documentation. There are limits with using Processing with arduino, as internally this still generates C/C++ code that is built into HEX before uploading it to the AVR. There are multiple C/C++ libraries for the arduino that allow for more functionality, but that’s a whole new blog post for another time.

The simplest thing to do is just go through the various example sketches (as they are called in the IDE) and just modify one of them and code up something that will continuously change the colors:


// Tricolor sketch : tekstop : Oct 7, 2010
// Constants
const int PINS[3] = { 11, 12, 13 }; // Pin numbers for Red, Blue and Green
// Globals
int ledStates[3] = { LOW, LOW, HIGH};
long interval[3] = { 500, 500, 500 }; // intervals for LED blinking for Red, Green and Blue
long previousMillis =0;
int I = 0;
int toggle(int state) {
if(state == LOW)
return HIGH;
else
return LOW;
}
void setup() {
for(int i=0; i < 3; i++) { // set the digital pins as output:
pinMode(PINS[i], OUTPUT);
}
randomSeed(analogRead(0)); // To get a random seed for the generator
}



void loop() { // This code runs continuously

if (millis() - previousMillis > interval[I]) {
I = (I+1) % 3;
previousMillis = millis();
interval[I] = random(1000);
ledStates[I] = toggle(ledStates[I]);
digitalWrite(PINS[I],ledStates[I]);
}
} //EOF

The code just sets up random intervals for the 3 led pins and just toggles them, producing different colors.

This is the most simple set up with this LED. I am just using digital pins that are sending either a high or a low pulse. This LED is capable of a lot more with PWM! Hold on to that!