Build Your Own SensorPurpose: Because the EV3 does not provide a General Purpose Input/Output interface (GPIO) like many of the wide-spread cheap microcontroller systems (Arduino, Raspberry Pi, Amtel AVR, PICDEM Lab, etc.), it is mostly used for moving robots and rarely for other typical microcontroller applications to control actors like lamps and other high power devices, relays, etc. In this series of examples you learn how to interface the EV3 with your own sensors/actors using the EV3 sensor ports. (This site is greatly inspired by the book from Michael Gasperi, Phillippe and Isabelle Hurbain: Extreme NXT. With thanks to the authors.) The additional external electronic circuitry is simple and only few knowledge of electronics and elementary elektro-mechanical skills are needed. But keep in mind that the principle of "trial and error" should not be applied to electronics development since a fault connection, interchanging an input and output line, a voltage that exceeds the maximum allowed value or drain too much current may destroy the external devices, the EV3 or both. So be extremely vigilant and check the circuitry before applying power to the devices.
Pin Description The NXT/EV3 sensor ports are numbered from 1 to 4. The plugs are similar to RJ-12 telephone connectors with six contacts. However because there is a plastic lock on the side of the connector, the plug is not fully compatible. To connect the sensor ports lines to an external circuitry it is recommended to cut a sensor cable in the middle, so you get two connection cables with the plug already fitted. If you strip off the isolation you find 6 wires colored white, black, red, green, yellow and blue (in this order). Usually the following naming is used:
Pin 1 - AN Pin 2 and 3 - GND1 and GND2 Pin 4 - VCC Pin 5 and 6 - DIGI0 and DIGI1
Typical Applications Digital output As stated DIGI0 - Pin 5 can be used as a general purpose digital output, but the driving capacity is very limited due to an internal resistor of about 5 kOhm. Use the method activate(boolean b) of the NxtLightSensor class to set the output to HIGH or LOW level. Normally a driver is needed to connect an external load to the Pin 5, This can be a simple general purpose transistor (2N2222) or a Darlington driver IC (ULM2801/2803) like in the following diagrams: Keep in mind that when you drive a solenoid relays, a protection diode should be used. If you drive a high voltage solid state relay, be very careful to avoid the hazard of an electric shock.
DIGI1 - Pin 6 can be used as a general purpose digital input. To get the current state, use the isPressed() method of the TouchSensor class. The LOW - HIGH threshold is about 2.5 V. Insert a 2.2 kOhm serial resistor to protect the input (max input voltage 5 V).
AN - Pin 1 can be used as a general purpose analog input. You get the raw input voltage like a NXT light sensor (class NxtLightSensor). The input voltage 0..5 V is converted with a 10 bit analog-digital converter. The reported light sensor raw value is 1023 for 0 V and 0 for 5 V because the input is inverted. Keep in mind that the input impedance is low (pull-up resistor 10 kOhm). You can make a simple test circuit using a potentiometer of about 1 kOhm and connect as follows:
Temperature sensor Already for the Lego RCX system, an thermistor based temperature sensor was available. Apparently the sensor uses a R25 = 10 kOhm thermistor with a 2.2 kOhm resistor in series. It can be connected directly between the Pin 3 - Red and the Pin 1 - White ports. When data are read as NxtLightSensor, the raw value reading is about 900 at -20 °C and 260 at 70 °C with little non-linearity. It is easy to build a home-brew temperature sensor based on the LM35 chip family. These 3 wire sensors outputs a voltage that corresponds directly to the measured temperature with 10 mV/deg. So at 0 °C the output voltage is 0 V and at 100 °C the voltage is 1 V. Because of the internal pull-up resistor of the EV3 analog input, a pull-down resistor of about 220 Ohm must be inserted. The mechanical construction is up to you, below some suggestions using a shrink-on tube to protect the sensor when immerged into a liquid. Much more accurate is the NXT temperature sensor (9749) that is supported in the EV3JLib in autonomous and direct mode by the class TemperatureSensor. The raw value must be converted to the temperature using a linear relationship:
which gives T = (1023 - v) / k with k = 2.05. In praxis more accurate values are obtained for k = 2.2. The following programs in Java and Python display the temperature on the EV3 screen. They run in autonomous or direct mode.
A demonstration of a typical control loop: Stabilizing the temperature This example is the prototype of a control loop in a feedback control system. To maintain a given nominal temperature TNominal at a certain location heated by a 50 W halogen lamp, the lamp is turned off, when the current temperature T is greater than TNominal (T > TNominal) and turned on, when T is smaller or equals TNominal (T <= TNomial). To switch the high voltage for the lamp (110 V or 230 V), care must be taken to avoid electric hazards. For security reasons a solid state relay (SSR) is build into an external boxed plug socket, so that the high voltage part is completely separated from the low voltage electronic circuitry. Because the maximum drive capacity of the EV3 digital output is very small, it cannot switch the SSR directly. But it is easy to obtain the necessary drive current by using a Darlington array IC mounted on a small circuit board (see the schematics above). Only 4 wires from the EV3 sensor cable are used: black (pin 2) and red (pin 3) wired together for the ground, green (pin 4) for VCC und yellow (pin 5) for the digital output. The program uses the NxtLightSensor class to activate/deactivate the digital output at DIGI0 and either the TemperatureSensor class to read the temperature of the Lego NXT temperature sensor (see Java program) or the NxtLightSensor class to get the raw values of a LM35 based homebrew temperature sensor (see the Python program) can be used. As you see the program logic is extremely simple. Again the same programs can be run in autonomous or direct (remote) mode.
Using I2C Expanders The I2C protocol is widely used to interface microprocessor based systems to digital or analog I/O devices. Only three wires are needed: Ground (GND), Serial Clock Line (SCL) and Serial Data Line (SDA). The EV3 acts as a I2C master and up to 128 external I2C slaves can be connected to a single EV3 sensor port, but most of the time only one slave is connected to a sensor port. The master selects the slave by sending an I2C address byte. The following bytes are data or control bytes sent from the master to the slave (WRITE mode) or from the slave to the master (READ mode). The lack of a general GPIO interface with the EV3 can be easily overcome by attaching as sensor one of the well-known I2C expander ICs, like the PCF8574/PCF8574A for digital I/O and the PCF8591 for analog I/O. Only two pull-up resistors are necessary as additional components. The expander ICs can be build with your own test or soldering board. Expander kits are also available (e.g. from mindsensors.com). The basic circuitry is the following:
A Demonstration for Digital I/O
The program code is shown in Java and Python. Again It runs in autonomous or direct (remote) mode.
A Demonstration for Analog I/O
The program code is shown in Java and Python. Again It runs in autonomous or direct (remote) mode.
There is a distinct difference in the repetition rate of the tone for the Java and Python programs that let you compare the execution speed. Of course the winner is Java in autonomous mode. Consult the Java documentation for more information about class I2CExpander constructor parameters and methods. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||