Day 23: Experimenting with a relay

Although the L293D is very handy, it has two important constraints: It is limited to 600 mA and it causes the voltage to drop.

The relay below has a 5V 40mA coil. It can deal with a charge up to 1A at 30V. It has the size of a 14-pin IC. Its lower part has a drawing explaining what every pin is for.

Relay_top Relay_bottom

In the following circuit, a 330 Ohms resistor is connected to the common pin, a green LED is connected to the normally closed pin and a red LED is connected to the normally open pin. The power provided is 5V.

Relay_GreenLed Relay_RedLed

As expected, the green LED lights when the coil is turned off and the red LED lights when the coil is energized.

Day 12: Playing with LEDs (III) – PWM and L293D

This time I replaced the red LED in the circuit I used on Day 11 for a ultra bright white LED.

The white LED requires more power than the Pi can provide, so I added the external power source used on Day 9 and a motor control chip, the L293D. I did the wiring based on this Adafruit tutorial but my Cobbler is connected in the inverted position.

This is the final circuit:

???????????????????????????????

Notes:

  • I used the following wire color guidelines: Black for ground, Red for Vcc, Yellow for data, and Blue for control.
  • The white LED can take up to 80mA and is connected to 100/3 = 33 Ohms resistor.
  • The green LED is connected to a 330 Ohms resistor.
  • I used an external 5V power source connected to the bottom-right corner.

The following picture shows the connections from another direction:

???????????????????????????????

This is the final result:

Day 11: LEDs and PWM using ServoBlaster

For the hardware, I used the same circuit presented on Day 10.

In order to create the PWM, I used the ServoBlaster library by Richard Ghirst. It is part of the PiBits project (look for the “Download ZIP” button in the right side of the page).

Although it is possible to use the ServoBlaster deamon from the command line, I wrote a program to gradually turn the LEDs on and off:

???????????????????????????????

C source code:

/*
blink.c
Written by Wilson Medeiros (clockeater)
Revision 1.0 - 2013-12-07

This software requires that the ServoBlaster daemon (servod) to be running:
sudo ./servod --min=0 --max=2000

ServoBlaster is a third-party library written by Richard Hirst
*/

#include <stdio.h>

void blink();
void setLeds(int green, int red);
void wait();

int main (int argc, char* argv)
{
  blink();

  return 0;
}

void blink()
{
  int cycles;

  for(cycles=0; cycles<10 ; cycles++)
  {
    int value;

    for(value = 0; value <= 2000; value += 100)
    {
      setLeds(value, 2000 - value);

      wait();
    }
    for(value = 2000; value >= 0; value -= 100)
    {
      setLeds(value, 2000 - value);

      wait();
    }
  }
}

/*
Set the pulse width for the leds
Values must be between 0 and 2000 (1 = 10 us, 2000 = 20ms)
The green led is wired to GPIO 18 and the red one to GPIO 23
Each one is wired in series with a 330 ohm resistor
*/
void setLeds(int green, int red)
{
  FILE *f;

  f = fopen("/dev/servoblaster", "a");

  fprintf(f, "2=%d\n", green);
  fprintf(f, "5=%d\n", red);

  fclose(f);
}

/*
Wait for 1/10 second
*/
void wait()
{
  usleep(100000);
}