So you might have noticed we didn't use one of those CircuitPlayground.something()
functions to get our analog value. Why not, and how did it work without doing so?
The answer to the second question is pretty easy - we just use the Arduino library. The Circuit Playground library is built (largely) on top of the Arduino library and 'hides' the details of using its various bells and whistles. However, you can still use the Arduino library if you want (and know what you're doing).
No real good answer to the other question. This could've been done, say with something like CircuitPlayground.analogRead()
or CircuitPlayground.giveMeTheAnalogPlease()
or whatever. But this would have likely done nothing more than just call the same Arduino function. So, guess it just never made the cut.
However, there are a few of the Circuit Playground sensors that are essentially the same as the potentiometer we used in this guide, namely the light sensor , sound sensor , and temperature sensor. They all output a voltage that varies with the physical quantity they sense. In order for this to be used in the Circuit Playground, those signals are fed into ADCs, just like we did with the potentiometer. And that's pretty much all there is to it.
Don't believe me? Well, take a look...
Light Sensor
Here's the circuit path for the output of the light sensor. The big red square that says 'THERMAL' is the brain of the Circuit Playground.
And here's the code for the CircuitPlayground.lightSensor()
function. (direct link to repo)
uint16_t Adafruit_CircuitPlayground::lightSensor(void) { return analogRead(CPLAY_LIGHTSENSOR); }
And here's the code for the CircuitPlayground.soundSensor()
function. (direct link to repo)
uint16_t Adafruit_CircuitPlayground::soundSensor(void) { return analogRead(CPLAY_SOUNDSENSOR); }
Pretty boring. Nothing fancy or magical. Both the light sensor and sound sensor just call analogRead()
for the pins they are attached to and return the value. And now you know why those functions return values from 0 to 1023. It's just the raw 10 bit analog to digital conversion.
And here's the code for the CircuitPlayground.temperature()
function. (direct link to repo)
float Adafruit_CircuitPlayground::temperature(void) { // Thermistor test float reading; reading = analogRead(CPLAY_THERMISTORPIN); //Serial.print("Thermistor reading: "); Serial.println(reading); // convert the value to resistance reading = ((1023.0 * SERIESRESISTOR) / reading); reading -= SERIESRESISTOR; //Serial.print("Thermistor resistance: "); Serial.println(reading); float steinhart; steinhart = reading / THERMISTORNOMINAL; // (R/Ro) steinhart = log(steinhart); // ln(R/Ro) steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro) steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To) steinhart = 1.0 / steinhart; // Invert steinhart -= 273.15; // convert to C return steinhart; }
OK, a bit more going on. But not really. Note that pretty much the first thing that happens is to read the analog value. The rest is just math to turn that into a temperature. In this case, that's more useful than the raw analog value. For the light and sound sensors, there's no really useful thing to convert to, so those just return the raw values.
Page last edited February 08, 2017
Text editor powered by tinymce.