Geeks With Blogs
Joaquin Jares Just another geek's blog

WARNING: I’m probably doing something terribly wrong here.

In my last post, I mentioned that using transistors you can do boolean logic. This is the fundamental basis for computers to work. I wanted to test the concept. In that article, I was using a single Netduino digital output to drive two LEDs. In this experiment, I’m using two digital outputs to drive four LEDs, using some transistors. Here’s the video:

Driving 4 LEDs from 2 pins using transistors

You can see the LEDs turning on in order when I press the switch. Following my first post, I coded an Encoder. The Encoder receives a single int does some boolean operations and turns it into outputs in the pins. The code is as follows:

``` 1     public class Encoder
2     {
3         private OutputPort[] outputs;
4
5         public Encoder(params Cpu.Pin[] pins)
6         {
7             outputs = new OutputPort[pins.Length];
8             for (int i = 0; i < pins.Length; i++)
9             {
10                 outputs[i] = new OutputPort(pins[i], false);
11             }
12         }
13
14         public void Reset()
15         {
16             foreach (var port in outputs)
17             {
18                 port.Write(false);
19             }
20         }
21
22         public void Encode(int code)
23         {
24             for (int i = 0; i < outputs.Length; i++)
25             {
26                 outputs[i].Write((code & ((int)System.Math.Pow(2, i))) > 0);
27             }
28         }
29     }
30 ```

The boolean logic at work is fairly simple, but just in case, this is what I’m doing:

For each pin, I’m computing 2^pinNumber, and then ANDing that to the value to encode. If I have for instance the value 5, that is represented by 101 in binary.

2^0 = 1 = 001 in binary

2^1 = 2 = 010 in binary

2^2 = 4 = 100 in binary

Doing the AND operation with this values, I can see that 5 will be encoded by turning on the first and last pin, and turning on the middle pin (which will represent 101, and is what I wanted to begin with). The maximum number I can encode is 2^numberOfPins – 1. With 2 pins, as I’m using in the sample, I can encode ( 2^2) – 1 = 3, so I can encode the numbers 0, 1, 2 and 3.

The Program then is very simple. I have a static int holding the current code and increment that when the button is released. The increment is constrained by 3 by using a boolean trick that only works for powers of 2, which is ANDing with the maximum possible number. Here’s the code:

``` 1     public class Program
2     {
3         private static BaseBoard board;
4         private static int code;
5
6         public static void Main()
7         {
8             code = 0;
9             board = new BaseBoard();
10             board.Button.Released += new NoParamEventHandler(Button_Released);
11
12             board.Encoder.Reset();
13
15         }
16
17         static void Button_Released()
18         {
19             code = (code + 1) & 3;
20             board.Encoder.Encode(code);
21         }
22
23     }
24 ```

The board, as you’ll expect, simply declares the Button and the Encoder:

``` 1     public class BaseBoard
2     {
3         public Button Button { get; private set; }
4         public Encoder Encoder { get; private set; }
5
6
7         public BaseBoard()
8         {
9             Button = new Button(Pins.ONBOARD_SW1);
10             Encoder = new Encoder(Pins.GPIO_PIN_D0, Pins.GPIO_PIN_D1);
11         }
12     }```

And that’s about all I need to tell you about the code.

On the hardware side I’m doing something (that is probably wrong) that involves using NPN transistors as an identity function of sorts and PNP as a negate instruction. That is, if an NPN receives a HIGH from the output it activates, and when a PNP transistor receives a LOW from the output it activates. So 0 (which is 00 in binary) will use two PNP transistors, the 1 (01) will use a NPN and a PNP transistors, the 2 (10) will use a PNP and a NPN transistor and the 3 (11) will use 2 NPN transistors. The problem with my design (I think) is the position of the LED and the current relating to the transistors. You will notice that when the rightmost LED is on, the next LED to the left is partly on, which is wrong. Here’s a nice schematic for the design (using an Arduino, but the Netduino is pin-by-pin identical in this case). I’m only using 2 2k resistors for the transistors (I’m using a lot more in the video) but it works exactly the same:

Of course the correct way of coding this is using a MUX, and I’ll explore that in my next post.

Comments on this post: Magic Transistor Boolean Logic