Using Digital Inputs & Outputs

1. Introduction

Digital inputs/outputs are very basic and common in any microcontroller. They are digital so you can easily relate with "1" and "0" ("on" and "off"). We can use them to control (turn on and turn off) light bulbs, motors, heater, electric horns...and accepting signals from varieties of switches and sensors. In this tutorial, we will talk about some of those devices that we can control or sense with basic digital I/O. Other devices are similar and based on the same concept.

2. Basic things about Digital I/O

Ansteron Board comes with 20 usable digital input/output (I/O) pins. Each pin can be used as either input or output and can be changed by user program. All digital I/O pins are inputs when start-up so you can start using right away. In the case a pin is used as output, you will have to change pin mode to output first.

Some components can be controlled directly with output pins, some cannot. Each output pin can handle a maximum current of 40mA. So if you want to control an LED which draws up to 20mA that is fine. You can even do 2 LEDs if you want. However, for a small DC motor, which can draw up to 150mA then you should not connect it directly.

Looking at component's label or datasheet will let you know how much current the thing may draw (or you will need basic Ohm law to calculate the current). If the thing needs more than 40mA, an external drive circuit is needed. So, in the case you need to control a toaster oven which could be 1000 W, at 120V AC, Ohm law says that the current could be up to 8,333mA. That is way too much so you will really need a drive circuit for that.

When using a pin as input, if the voltage of signal is higher than board's voltage (5V) then the pin may be damaged. That is not a case if you keep everything the same voltage as Ansteron Board. However, if that is a case, some extra components is needed to accept those signal and we will discuss that below.

Note: It is implied that Ansteron Board in the example circuits below is powered by either USB port or DC input and GND of the board is also Ground of the circuit. Supply voltage (+5V) is either external or from +5V pins of Ansteron Board.

3. How do they work?

Input pins

An input pin will give value "1" or "0" when the program read it. We also use "high" and "low" to mention those two values. Function read_pin(PIN_xx); returns current state of input pin (xx is pin name).

You can also use pin_is_high(PIN_xx); and pin_is_low(PIN_xx); which will be true if the pin is high or low respectively

In electrical meaning, we are talking about voltage. "High" value means the voltage of the input pin is higher with respect to ground (GND). And "low" means voltage is as low as ground. For Ansteron Board, voltage below 1.5V will be read as "0" and voltage above 3.5V is "1". However, those values are approximate based on nominal operating voltage (5V) and room temperature. In your project, just make sure that the voltage of low signal is as close to 0V as possible and a high signal is close to 5V.

In some cases, you may need to have a "level keeper" which is a large value resistor (usually 10K or more) connecting the pin to GND or V+. For mechanical switches like a push buttons, they will leave the input pin "floating" when they are open and since those inputs pins are pretty sensitive, a touch of your finger may change their reading value from "0", supposedly, to "1".Besides, some components are "active low" which means when they're active; they tide their outputs to GND instead of V+. In that case, you may need a resistor connect to V+, which is called pulled-up resistor. Diagram below is another way to connect a push button; the reading will be "0" when it is pressed and "1" when it's not.

Every pin on Ansteron Board have a weak pull-up (about 50K) can be enabled/disable by software. When a pin is input, calling function set_pin(pin) will enable internal pull-up. Function clear_pin(pin); will disable pull-up.

Mechanical switches like push buttons may need decoupling capacitors to avoid short pulses of signal happening when they change state. This is only needed if button's state is checked at a short interval. The graph below shows what could (not always) happen when a push button is pressed.

Example circuit:

Output pins

The diagrams above shows internal structure of one output pin. There are two switches, if a value "1" is written to a pin, the upper switch will close and the lower switch will open, the pin is connected to V+. When value "0" is written, the upper switch will open and the lower one will close, that connects the pin to GND. Those red arrows show the current flow.

So, when the outputs are written to "0", that doesn't mean they are disconnected. If you connect an LED like the circuit below, you will see that the LED lights up when "0" is written to the pin.

In your program, use function set_pin(PIN_xx); to write value "1" and clear_pin(PIN_xx); to write value "0" (xx are pin names).

4. Drive circuits

If you just use output pins to send signals to other components or to drive an LED, things a pretty straight forward. In the case you need a drive circuit, examples below show you how.

a. Transistor drive circuits

Transistors are used for many purposes but we only look at them as switches in this tutorial. They can be coupled with output pins to drive other devices or components that draw more current than a pin can handle. Small and low cost transistors can handle current up to 800mA; larger transistors can work with currents up to 50A. These transistors can only work with DC (direct current), for AC (alternative current), you can use relays instead (they are discussed below).

There are a few types of transistors, we will look at common ones are NPN, PNP bipolar junction transistors (BJT) and N-channel, P-channel field-effect transistors (FET). They have different characteristics and the way they work, for driving things that are 800mA or less, BJT are good enough. For currents from 800mA to 10A, FET are more appropriate when using with Ansteron Board.

The cool thing is that you can drive things that are higher voltage (or less) than Ansteron Board with these transistors. So, you can control a 12V light bulb with your board running at 5V. Those below are examples on how to use them, note that different type of transistor has different symbol in schematic although they look similar in real life. Pin function of each transistor type can be found in their datasheet.

NPN type transistor: when output pin is "1", the transistor active and closes the circuit.

PNP Transistors: When output pin is "0", the transistor will become active and drive the circuit. This is opposite to NPN transistors. Note that the transistor may not turn off when the pin is "1". In that case, making the pin input to let the 10K resistor pull signal line closer to +12V will turn off the transistor

Note that for PNP transistors, consideration must be made if drive voltage is much higher than Ansteron Board voltage (5V), you may need a resistor value more than 1K for such cases. If drive voltage is too low, PNP transistor may not work.

N-Channel FET: In this example, transistor will be active when output pin has value �1�.

P-Channel FET: Similar to PNP transistor, P-channel FET will be active when the output is "0" instead of "1". Be aware that if drive voltage (for load) is below 4V, this transistor may not turn on. To turn off the FET, making the pin input to let the 10K resistor bring the signal line close to +12V. If the pin is left as output and write "1" into it, the voltage is only 5V which may not make the FET turn off.

b. Relay

Relays are electromechanical switches that can be coupled with output pins to control things that draw a large current, usually from 2A to 100A, either DC or AC. Depends on size and type of the relay, you can connect it directly to output pin or you will need a small transistor to drive it. Note that in either case, you will need a diode to protect output pin against surge (voltage spikes) that occurs when turning the relay on and off.

If your relay can be driven with 5V and 40mA then you can set up your circuit as below:

Otherwise, a NPN transistor can be used:

Caution high voltage: To avoid electric shock, remove high voltage power supply while working by hand

5. Optical Isolator

For both inputs and outputs, optical isolators may be needed to interface with devices that are electrically noisy or their power supply and ground are different than Ansteron Board. The main idea is to avoid interferences from the environment, especially industrial environment that may cause the CPU of Ansteron Board to malfunction.

A simple circuit that uses optical isolator for an input.

Isolate Ansteron Board and a high power relay:

6. Note

IO pins on Ansteron Board is not designed for high load, they are better at high speed signals. Avoid connecting any IO pin to voltage higher than 5V, which may damage the pin.