Friday, March 18, 2011

Source Code Pretty Printer for Blogs

I found a nice pretty printer for code on blogs for the lazy like me: http://formatmysourcecode.blogspot.com/

Thursday, March 17, 2011

The Curious Case of Pull-Up / Push-Down Resistors

What are pull-up resistors? And push-down resistors?
While trying to find answers to these questions on-line, I could only find sources too advanced for me to understand (I'm still a newbie after all).

But only after coming across some phenomena while playing with my RGB LEDs, did I understand what's the idea.

You see, one might think that if a circuit is open (like a switch) that the potential (voltage) is 0V, but actually because of the static electricity in the environment, that isn't entirely true.
There are components that need so little energy to work, that they might be influenced by that electricity.

What happened to me was that while having a LED connected only to GND, when I touched the other lead, it would light up a little.
When I tried to use a push button in a circuit connecting only one end to an output pin and the other end to an input pin of the Arduino, the Arduino would read strange values either I did or didn't press the push button.

So, these pull-up / push-down resistors are to compensate for this extra energy, forcing the circuit to a stable state.

In the push button case, I had to use a resistor in parallel with the input pin of the Arduino, so that when the button isn't pressed, it would always read 0V (LOW).

The difference between pull-up and push-down is where you use that resistor, either connecting the component to GND or to the source.

I hope to have simplified it enough. What do you think?

RGB LED Part II

After some absence, I finally got some weekend time to play with my electronics.
Now that I have 100 new RGB LEDs, it’s time for some experiments.

I wanted to know how to control the colors emitted by this LEDs so here’s how I did it.

First I searched for a datasheet and found this one on Sparkfun.
According to it, the recommended current is 20mA, and the voltages are 2.0V, 3.2V and 3.2V for red, green and blue. As I wrote on my last post, each color has a different lead, and there is another for Ground (GND). These are common cathode LEDs, as it receives energy on the leads for the colors and all that energy is returned to the circuit on a single lead.

As I source all my projects with the Arduino connected to the laptop (as it already is a regulated power source), I calculated the resistors for a 5V source power.

V=R.I
Red --> (5 – 2.0) / 0.20 = 150 Ohm
Green / Blue --> (5 – 3.2) / 0.20 = 90 Ohm (The closest I have is 100 Ohm)


Connecting all wires, we can see that 3 different tiny LEDs lit up inside our 5mm RGB LED as you can see below.

Also we can see that we can’t count on a mixed color far from the LED, as the three parts point to different directions, although there might be some ways to try to improve that situation.

So the LED lights up. Big deal! Let’s play some more...

The next step is to be able to combine the three colors into making new colors.

First I tried to make a simple program on the Arduino using a (very, very poor) simulated PWM. Basically I would light up only one color at a time and tried different delay() times, to control the ‘quantity of color embedded’ in the final result. That idea came from the LED matrix project, as some kind of multiplexing.
Please, don’t waste your time on something like this.

After some online searching, I saw that the best way is to use ‘real’ PWM, as in using the PWM pins in the Arduino and analogWrite() the value (0-255) for each color. This controls the voltage at the pin, altering the luminosity of the LED. It is best to keep all color lit at the same time (not using delay()).

So now we can programmatically define the color we want to obtain, in theory. Again, with clear LEDs, it is kind of hard to observe a good mix of the light emitted by the three ‘sub-LEDs’.

But isn’t it cooler to be able to mess with those values by hand? So enter the potentiometers (pots).
My first try was to use 3 pots, to control each color. As two of them aren’t ‘breadboard friendly’, you can see below what a mess it is to connect it all.


But then I thought: “What a good opportunity to try to use push buttons.”
So I replaced the 3 potentiometers by 3 push buttons, and altered the Arduino program to cycle through the values for each color.
A simple control to avoid altering the values too much in one push was to have a small delay() after the digitalRead() of each button.




You might notice some resistors connected from each button to GND in parallel with the reader pins of the Arduino.
Those are push-down resistors.

And here is the code:
int R = 9;
int G = 10;
int B = 11;

int vR = 0;
int vG = 0;
int vB = 0;

int T = 200;
int d = 15;

void setup() {
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
pinMode(7, OUTPUT);

pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);

digitalWrite(7, HIGH);
}

void loop() {
if(digitalRead(2) == HIGH)
{
vR+=d;
if(vR>l)
vR=0;

delay(T);
}

if(digitalRead(3) == HIGH)
{
vG+=d;
if(vG>l)
vG=0;

delay(T);
}

if(digitalRead(4) == HIGH)
{
vB+=d;
if(vB>l)
vB=0;

delay(T);
}

analogWrite(R, vR);
analogWrite(G, vG);
analogWrite(B, vB);
}


So to end it, I wanted to create a demo to cycle to all colors as simply as possible. I found ‘Hue-controllable RGB LED lamp‘ that shows how to cycle through the colors using only a Hue value.
I tried to use the conversion code directly from http://www.easyrgb.com/math.php?MATH=M21#text21, but all the values were quite wrong, so I lazily used the code akgraphics provided on the first post.
As the code was kind of slow because of lots of (unnecessary) math, I reduced it to:

void setup() // run once, when the sketch starts
{
//Serial.begin(9600); // set up Serial library at 9600 bps
}

void h2rgb(float H, int& R, int& G, int& B) {
int var_i;
float S=1, V=1, var_1, var_2, var_3, var_h, var_r, var_g, var_b;

var_h = H * 6;
if ( var_h == 6 ) var_h = 0; //H must be < 1
var_i = int( var_h ) ; //Or ... var_i = floor( var_h )

//var_1 = V * ( 1 - S );
var_1 = 0;

//var_2 = V * ( 1 - S * ( var_h - var_i ) );
var_2 = ( 1 - ( var_h - var_i ) );

//var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );
var_3 = ( var_h - var_i );

if ( var_i == 0 ) {
var_r = V ;
var_g = var_3 ;
var_b = var_1 ;
}
else if ( var_i == 1 ) {
var_r = var_2 ;
var_g = V ;
var_b = var_1 ;
}
else if ( var_i == 2 ) {
var_r = var_1 ;
var_g = V ;
var_b = var_3 ;
}
else if ( var_i == 3 ) {
var_r = var_1 ;
var_g = var_2 ;
var_b = V ;
}
else if ( var_i == 4 ) {
var_r = var_3 ;
var_g = var_1 ;
var_b = V ;
}
else {
var_r = V ;
var_g = var_1 ;
var_b = var_2 ;
}

R = (1-var_r) * 255; //RGB results = 0 ÷ 255
G = (1-var_g) * 255;
B = (1-var_b) * 255;
}

void loop() // run over and over again
{
for(int val=0;val<1024;val++)
{
//Serial.println(val);
float h = ((float)val)/1024;
int h_int = (int) 360*h;
int r, g, b;
h2rgb(h,r,g,b);

analogWrite(9, r);
analogWrite(10, g);
analogWrite(11, b);

delay(10);
}
}

Monday, January 24, 2011

RGB LED

This weekend I started playing with a RGB LED. This LEDs have 3 separate light emitting materials. One for red (R), other for green(G) e the last for blue(B).

As you can see in the image below, there are 4 leads. This LED has a common cathode, which means that all 3 parts share the connection to GND.


From up to down, we have Blue, Green, Ground and Red.

To start experimenting, I searched for a generic datasheet, as I don't know any reference for this LED. It recommended a 20mA current and stated that the typical voltages are 2.0V (Red) and 3.2V (Green and Blue).
As I was drawing power to the breadboard from the Arduino (only for power), I did the math for the resistors.

V=R.I
(5-2)/0.02=150 Ohm
(5-3.2)/0.02=90 Ohm

There where 3 150 Ohm resistors in the anti-static bag the LED came in, but I decided to use 100 Ohm resistors for green and blue.

So, the simplest test was to light each one at a time:

Check!
Check!
Check!

Next step, how about lighting all at once?
It would be nice, but there was a problem. When I disconnected the GND from the breadboard, I accidentally let it touch the lead for the red color. Without a resistor to limit the current passing at the moment, it burned out in a second.

So what could I do with a 'limp led'? I tried to combine the green and the blue colors to see the effect, but I ended up getting a slightly lighter blue.
When I read the datasheet I noticed that the brightness (measured in candela) of the 3 colors isn't the same. It also looks like the blue color is more diffuse (spreads all around) as the green light appears more directional. I filmed it try to show what I mean, but the colors aren't very noticeable.



The good news is that I ordered a 100 RGB led pack on eBay, so if all goes well, I will try again soon.

One hint: I read that PWM is the way to go in order to combine colors...

P.S. The LEDs have arrived, all working and with some free resistors. Thank you giorgio11185!

Wednesday, January 12, 2011

Make: Presents

I like Collin Cunningham's videos a lot. They are fun and simple and give me motivation for my home projects.

Here's a compilation of some of his videos about the basics of electronics.

Saturday, January 8, 2011

Piezo 101

To learn how to use a Piezo Buzzer I followed this tutorial.
They say that piezos have polarity, but the one I have doesn't show what it is, or if it has any.

What is polarity?
It is best explained on Wikipedia, but basically it defines a single right way to connect a component to the circuit, depending on the direction of the current flow. Components like resistances don't have polarity, so they can be connected either way. Polarity matters for most electrical objects like diodes (LEDs are diodes) or speakers.


I couldn't find a datasheet to the exact name (PKM22EPP-40) and neither on the manufacturer (muRata) datasheets for similar components.
But after reading this I found the explanation for the reference name (not helpful...).

So I tried the program supplied using a 330K resistance so that I could test for polarity reducing the risk of burning the piezo.
It generated a very low sound either way I connected it.

With a 10K resistance, the sound is louder, but still kind of weird.
Then I tried 470 Ohm and 47 Ohm with the same results.

So I dropped the resistances, and the sound isn't much louder, but still as annoying.
And this piezo has no polarity after all.


(Something wrong with the processing of the video from Blogger that makes the sound out of sync...)

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

So for the next test I tried this example from Adafruit, and got better results. The sound is clearer, and not as annoying.

The basic difference from the previous example is the use of digitalWrite instead of analogWrite to the piezo.
But what difference does it make? Well that is [TO BE SEARCHED].

Wednesday, December 15, 2010

Andruino (Cont.)

I've done some searching about the possibility to being able to compile arduino sources on an Android device and upload it to the Arduino via USB.

Well, I've learned that the stock Android doesn't support the use of the USB port, so it could only be done by rooting your device.

For this to work your device should also be able to use USB OTG, as it would have to be the server in the connection to the Arduino.

Here's the best shot so far to be able to implement this idea.

However it is possible to communicate via bluetooth or WiFi with the stock Android, but for the Arduino that would require an extra shield (XBee) or a specific board (Arduino BT).
There is a project for this, called Amarino. It is designed specifically for communication between Android and Arduino by Bluetooth.

Also I found that the stock Android source for HTC phones is shared here.
This can give people hints on how to circumvent the software limitation.

If rooting your device is an option, Cellbots is the way to go.