Power switch state


i am playing with a relay switch, i have 2 leds; 1 for OFF and 1 for ON.
when i am pluging the power to the imp both of them are active, the question is how do i tell the imp the first state of the pins when it is powering up?

another quick one, if i connect a led to pins 8 and 2 the led is constantly on (low power), even if i am writing a small basic program where those pins a are not involved at all, any ideas.

(Yes, Yes i am a noob :))


you just add a line somewhere in your code saying the state of each pin.
after you configure it as you wish, you write:
hardware.pinX.write() with 0 or 1, whatever your configuration is.
depends on your setup,
In my project, I have two leds involved also, one is pin8 and the other is pin9.
so both led’s positive leg is attached to pin8 or pin9, and the other to GND.
I do:
// Pin 9 = Green LED hardware.pin9.configure(DIGITAL_OUT); hardware.pin9.write(0); // Pin 8 = RED LED hardware.pin8.configure(DIGITAL_OUT); hardware.pin8.write(1);
so the initial state is RED, since I write “1”, which gives high voltage (3.3v, up to 4mA) on pin8.

another thing you should know, there are permanent tables, which allows you to save data on the server & restore it back, if the imp was turned off for some reason and you wish to preserve its state.

about the behavior of unconfigured pins, I dont know what happens, but if you dont use them, so simply dont use them :slight_smile:

You can either use current sourcing or current sinking …

For driving LED’s or transistors/TTL chips, etc., you can also “sink” the output instead of “driving” it. So your configure would look like:


The LED w/resistor is between the output pin and the 3V3 pin.
In this case, when you output a zero (0), the LED is ON, and a one (1) is OFF.

Which one is better? I’m not sure. Someone would have to answer the questions about whether or not the Imp is better at “sinking” or “sourcing”. I’m old-school electronics, so I’ve always been used to current “sinking” with TTL and other transistor applications. The devices I’ve used in the past were usually better at “sinking”.

mlseim, im not fully familiar with pullup
so just to be sure,
writing 1 to the pin9 => Vpin9 = HIGH (about 3.3v) so no voltage drop on the led, thus its OFF
writing 0 to pin9 => Vpin= LOW (~0) so there’s voltage drop, but I assume that since the imp pin can tolerate around 4mA (or thats a mistake?) I need to calculate the resistance of the led, and together with the voltage of its other leg, add a resistor to lower the current on that channel?

There should always be a resistor in series with an LED. And I mean each LED should have its own resistor (which lead it’s on doesn’t matter, it’s in series). If you don’t have resistors, that might be your initial problem.

You are correct about your 1/0 logic vs the voltage drop.

The value of resistor depends on the LED, but you can start with a 330 ohm resistor. You might try 220 ohm, 150 ohm, or somewhere around that. Basically, you’re reducing the current flow through the LED. That flow will affect the LED brightness and also affect flow through the imp. The polarity of the LED has to be correct also (flip it around if it doesn’t light).

I never calculated the mA on mine. I’ve lit 3 LED’s each having a 330 ohm resistor on one of its legs. No variation in brightness when 1 is on or 3 are on.

Someone on this forum with a good knowledge of the actual chip hardware can attest whether or not the imp is better at “sinking” a pulled-up load (it can sink more current than it can source?)

If im sourcing the led (connecting between pin9 and GND), why would I add a resistor there?
what is the normal voltage drop for led? I see on wikipedia that RED is about 1.8v, so indeed I need to drop the rest (1.5v) on a resistor, so
I=4mA, V=1.5V => R=375ohm.
what about green led?

and what happens when I drop the ntire 3.3v on the led?

Why not just going the other way around, if we use ANALOG_OUT, so we write 0-1 in order to output 0-3.3v, so for 1.8V we need to write a value of 0.5 to the pin?

In such usage, does the led has maximum brightness, or using PWM would do anything different?

You can’t use analog_out to drive an LED, it can’t drive that much current.

If you put the LED across a GPIO and GND (or GPIO and VCC) then you may see it lit from the pin leakage, possibly.

Try 200R on the green LED.

Max brightness for an LED is at 20mA typically. You would need to use a FET to do that as the imp can’t supply that much from a GPIO.

I see. Ok, so i will use my 3.3v power supply to source the led. does it matter if i use DIGITAL_OUT_OD_PULLDOWN and write o=power off, 1=power on? it makes more sense…
so with my 3.3v power source (the one i attach to vin is already 3.3v), and i have a resistor and a led. what current can the imp gpio tolerate?

If you are connecting anode of the LED to 3.3v, then through a resistor to an IO pin, you do not want to use DIGITAL_OUT_OD_PULLDOWN, because even when the pin is written to 1, the pulldown will cause current to flow through the LED and light it up.


The imp GPIOs are specced at 4mA.

Hugo … that’s good to know, thanks.

What is the difference between digital_out and digital_out_od_pullup ?

DIGITAL_OUT is push-pull (ie, it will ground the pin if you write 0 to it, and connect it to 3.3v if you write 1 to it).

DIGITAL_OUT_OD_PULLUP is open drain (the OD) with a pull up. This means if you write 0 to it, it will connect the pin to ground (same as digital_out), but if you write 1 to it then it will just “float”. The “PULLUP” bit means it applies a weak pull-up to this signal, effectively connecting the pin to 3.3v with a 50-100ish k resistor. This will look high if you checked it with a multimeter, but won’t be able to drive any load.