Shopping Cart


Your Cart is currently empty.

Home The Insider Blogs Electronics 101
Electronics 101

It's time to turn things around, folks! After discussing how to send signals to LEDs and such, I think it's only natural to start looking into inputs: reading signals from a Digit-LS into your computer. But before we do so I feel the urge to explain a few things about electronics. Just for those who are more on the computer side of things.

So far we have been switching output pins from 0V to 5V and back and flashing some LEDs with that. But if we want to read a signal, let's say whether or not a button is pressed, how does that work out electrically? First of all, you can not just set the pin to 0V and then connect a pushbutton to it. At some stage the pushbutton will try to put 5V on it while the Digit-LS will try very hard to keep it down to 0V. This creates what we electronicians (?) call a 'short circuit' and that is bad news. Both your pushbutton and your output pin will fight for putting their desired voltage on the pin. We can make a more general statement out of this: If some electronic circuit, no matter how simple or small, is connected to a pin of the Digit-LS, then either the Digit-LS or the electronic circuit can control the voltage of that pin. Otherwise smoke will appear.

The microcontroller that is part of the Digit-LS has a solution for this: tri-state mode. In addition to setting the pin to 0V or 5V, it can also be put in a high-impedance state. This essentially means that the microcontroller will not set the pin to 0V or 5V, but it will let that pin happily swing with whatever voltage the outside circuitry puts on it. The microcontroller can however determine if the voltage that is set on the pin, is high (5V) or low (0V) and make it read as 1 or 0 respectively. So let's put that in code. First of all, you need to switch the pin to be used (let's say A0) into tri-state.

TRISAbits.TRISA0 = 1;

Now we can read out the voltage of that pin like this:

a0input = PORTAbits.RA0;

If the input on pin A0 is low, a0input will read 0. If it is high, a0input will read 1. To make it somewhat functional, you could put a led with a resistor on pin A1 and then program something like this:

TRISAbits.TRISA0 = 1;                  // make A0 input pin
TRISAbits.TRISA1 = 0;        // make A1 output pin

while (1) { 
   PORTAbits.RA1 = PORTAbits.RA0;      // Switch LED on or off according to input pin
}

So now we are using a microcontroller to switch a LED on or off with a pushbutton. How fantastically useless is that? Well, the point is of course to see how you configure the Digit-LS and read input levels on its pins, so as long a you can understand that, it has been rather useful.

The general rule about input pins and smoke as stated above, is always valid. So remember when designing an application for your Digit-LS, to make sure that any pin that you will put a voltage on from your circuitry, is put into tri-state before you put that voltage on. This usually means that the pin needs to be put in tri-state as soon as your circuit is powered and the microcontroller starts executing code. Now the good news is that, by default, all pins on the Digit-LS are configured as tri-state pins on power-up. So if you just make sure you don't configure any pins as output pins while your circuit might put any voltage on it, chances of you seeing smoke coming from it are substantially smaller!

This may all sound simple and trivial, but I can assure you these mistakes are made often, even by professionals. Hey, even I got it wrong more than once! So sleep on this for a week, then next week I will get into the details of how to connect a pushbutton to an input pin the right way. That is not entirely straightforward for those who have not done that before. Oh, and by the way: Next weekend starts on Thursday already and yours truly is outdoors for the entire weekend. So my next blog might not make it online Monday morning sharp. Will try to make it at least before Tuesday morning.

The Insider

Last Updated on Wednesday, 03 June 2009 10:58
 
Copyright Sofrides IO