Trying to read analog pin2 on explorer kit and tweet value

New electric imp user here. I’m trying to add analog reading pin2 water sensor to the sensor tweet exercise in the explorer tutorials.

I’m missing something since I don’t understand the reading versus conditions part of the function. Any help would be appreciated:

#require “HTS221.class.nut:1.0.0”
#require “LPS22HB.class.nut:1.0.0”
#require “WS2812.class.nut:3.0.0”

// Define constants
const sleepTime = 30;

// Declare Global Variables
tempSensor <- null;
pressureSensor <- null;
led <- null
waterSensor <- hardware.pin2;

// Define functions
function takeReading(){
local conditions = {};
local reading =;
conditions.temp <- reading.temperature;
conditions.humid <- reading.humidity;
reading =; <- reading.pressure;
reading =;

// Send 'conditions' to the agent
agent.send("reading.sent", conditions);

// Flash the LED

// Set the imp to sleep when idle, ie. program complete
imp.onidle(function() {


function flashLed() {
led.set(0, [100,0,28]).draw();
led.set(0, [0,0,0]).draw();

// Start of program

// Configure I2C bus for sensors
local i2c = hardware.i2c89;

tempSensor = HTS221(i2c);

pressureSensor = LPS22HB(i2c);

// Configure SPI bus and powergate pin for RGB LED
local spi = hardware.spi257;
spi.configure(MSB_FIRST, 7500);
hardware.pin1.configure(DIGITAL_OUT, 1);
led <- WS2812(spi, 1);

// Take a reading

“conditions” is the table that you send to the agent. So you’ll want to add the water sensor reading to the table. Rather than
reading =;
conditions.water <-;

In the agent you’ll receive a container with all the previous slots, plus one called “water”.

1 Like

Thank you coverdriven. That worked, I am sending a value to twitter now. But, that value doesn’t seem to change - if I pull pin2 down to ground pin, or if I pull it up to 3.3V pin, I am still getting a relatively constant value of 5160 plus or minus 50.

I verified the pin should change values by using a different example program that builds a html file to watch the pins change state (ground is like 16 and 3.3 is like 65000).

Is there something I need to do to enable the ground and 3.3V pins? Are they “off” during the readings?

Ah, I know what’s happening…

The WS2812 driver reconfigures SPI, which takes over pins 2, 5 and 7. It doesn’t do this until you instantiate the driver with:

led <- WS2812(spi, 1);

…which then tramples over your watersensor configuration.

Really, the driver should configure the SPI with only “MOSI” driven, which wouldn’t mess with your setup.

The fix should just be to move the waterSensor.configure() to after the led driver instantiation.

That worked. Thank you. Difficult to understand these details.

That one was certainly subtle… as I said, the 2812 driver needs fixing so that it doesn’t touch pins it doesn’t need.