Sensor Workshop: Visualization of Sensor Data Over Time
The assignment was to take a sensor with an analog output and track it over time, displaying the resulting information in a way that was meaningfully connected with the information being sensed.
I wanted to play with some sensor I hadn't played with before, but a dig through my toolbox didn't turn up any likely candidates, so I pulled out the old photoresistor standby.
Schematic
The schematic was a pretty standard photoresistor setup, including an LED so I could quickly see the output of the photoresistor.

note: as the light goes up, the resistance goes down, and the output voltage goes up
Arduino
The arduino code was pretty straightforward. I prefer to keep the microprocessor code as simple as possible, and do the calculations on the computer side
int prPin = 0; // Analog input pin that the photoresistor is attached to
int prValue = 0; // value read from the photoresistor
int led = 9; // PWM pin that the LED is on.
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
//read the photoresistor and divide the value (from 0 to 1023) by 4,
//so it can be sent as a single byte
prValue = analogRead(prPin)/4;
Serial.print(prValue, BYTE); //send the serial input as a byte
analogWrite(led, prValue); // PWM the LED with the pot value
delay(20); // wait 20 milliseconds before the next loop
}
Processing
The processing code can be found here. The applet online runs using the mouse's X location as the input, so you can see how it looks. The source code contains the code for reading the photoresistor, commented out. I also added (commented out) code for reading the brightness from the middle pixel (I think) of a camera capture from (oh for example) the built-in isight in a macbook. The camera capture is interesting, because the exposure autoadjusts, so it's not so obvious how to bring the brightness up. Although now that I think about it, it might be fun to have a running read of the brightness across the entire image array... hmm, maybe I'll go do that now.
Thoughts
Basically this visualization does two things:
it displays brightness in several redundant ways, and it compares running raw data against a weighted average.
The brightness is displayed:
- in a traditional graphical format as the height of a line, which ends up looking like a waveform because of the way the graph is centered;
- as the brightness of the fill of the graph, in linear relation to the resistance of the photoresistor (which as best as i could figure is more or less linear, although it might have a nonlinear response at its low end (or i might just nonlinearly block light as i get super-close to the top of the resistor);
- and as the brightness of the entire display, also in linear relation to the resistance, but as a very stretched out moving average.
I'd like to use this sort of setup to visualize the data from several different sensors at once, so that i could use different visual cues to display nonredundant data, and see how to make that intelligible.
The difference between the raw data and the moving average is pretty low for this sensor, because the sensor itself seems pretty stable, and i didn't make any light fluctuations that were fast enough to cause spikes that would be edited out by the average. Mostly, the moving average is a smidge smoother, and changes come a smidge later. That's a technical term there. I don't think this sensor in the context in which I used it is the best choice for displaying the benefits of that sort of filtering.
The really slow de facto moving average of the overall brightness of the display was interesting, because it changed my perception of the time scale that I was looking at. Rather than filtering out spikes, it filtered out whole seconds of changing light, and only registered a clear response when the light settled into a new ambient brightness level. In a way, it changed the nature of the sensor.