Computing stuff tied to the physical world

Measuring distances

We’re making progress, but so far the project is not much more than a blinking LED. There is however something odd going on when you look at the logic that does the blinking: it’s not really blinking anything, but rather it’s sending out a short message to the serial port.

As it so happens, the LED is connected in such a way that it’ll light up whenever the serial output pin sends data. The reason to do this is that we can now take it one step further and actually re-use that same pin to send information to a connected host PC – via FTDI.

Let’s do that now:

DSC 4841

There’s an extra grey wire tied to an FTDI pin, and there are three extra wires plus a 100 kΩ resistor – these are used to connect the IR sensor. More about that part shortly.

If you now start up a serial terminal program, and connect the FTDI board to the breadboard, then you’ll see this output coming from the µC:

$ lpc21isp -termonly -control x /dev/ttyUSB* 115200 0
Terminal started (press Escape to abort)

Hello world!
Hello world!
Hello world!

This is extremely convenient, because we now have a way to report and debug what’s going on, with the µC sending us text messages over the serial link.

The comparator

The LPC810 has a ladder-network + comparator, as mentioned before. By “tapping” the ladder, we can determine the voltage level of an attached signal, such as the IR sensor.

For this to work, you need an LPC810 which has been pre-loaded with the proper software. If you uploaded the 1-blink software yourself – now is the time to upgrade to 2-analog.

Ok, plug the chip back into the breadboard, and verify that the IR sensor has been attached as shown above. Those three wires should be connected as follows on the sensor side:

DSC 4839

Power it up, with the serial port connected, and you should see something like this:

$ lpc21isp -termonly -control x /dev/ttyUSB* 115200 0
Terminal started (press Escape to abort)

analog = 32
analog = 22
analog = 0

When measuring 0V, the readout is 0. When measuring 3.3V, the readout will be 32, so that’s roughly 0.1V per step. Note that there are ways to greatly increase the resolution with just one extra I/O pin (see this app note from NXP, as ZIP archive including sample code). However, in this project 5-bit resolution will be fine.

With the IR sensor connected, you can now wave your hand in front and observe the voltages reported by the sensor. As you will see, the voltages are between 0 and 3 V, and are inversely related to the distance – the further away, the lower the output voltage.

On a typical setup, the readings should be 2 or 3 without obstruction, increasing up to 29 or 30 when an object reflects the IR beam at about 5 cm away. First readings should start at about 80 cm, and at 50 cm the readings should be 5 or 6. Total range is ≈ 0.2 .. 3.0 V.

Note that these values are a very nice fit within the allowed 0 .. 3.3V range of the LPC810 (even though the IR sensor is powered by 5V).

If you have a scientific inclination and make a plot of distance versus voltage, you’ll notice that the relationship is non-linear. It’s more precise in the near range than in the far range.

Why the 100 kΩ resistor?

As you saw in the wiring diagram, there’s an extra 100 kΩ resistor between pin 5 of the LPC810 and the yellow IR sensor output wire.

This is an essential detail to get things working, and it’s due to the very low pin count of the µC. By necessity, pins will be re-used for different tasks at different times. Pin 5 is LPC810’s “ISP pin”, and must be high when the µC comes out of reset, except for uploads. Since we’re not uploading in this circuit, it has to be high on power-up, period.

Unfortunately, the output of the IR sensor starts out being low on startup, rising to the proper voltage after dozens of milliseconds: ages in µC-time, and too late for the LPC810.

To resolve this, the sensor is attached through a relatively high resistance of 100 kΩ. It’s high enough to not let the IR output interfere with proper startup, but it won’t affect the actual readings much once we switch the comparator pin into high-impedance input mode.

Problem solved – though this did lead to a bit of head-scratching initially!

Ok, we now have a working sensor setup. To get the LED to blink at a corresponding rate instead of sending text to the serial port, you could upload the 3-distance software into the LPC810. It’s a minor and fairly obvious change to the previous code.

We have lift-off – great! Now let’s figure out how long the GPA setup can stay in orbit…

[Back to article index]