Turning on a LED

Connect LED to obniz and turn the LED ON and OFF from your smartphone.

Connect

Did you already configure Wi-Fi?
If not, that is what you should do first. Please do so here QuickStart

LED of course lights up by supplying electricity.
It has two pins, anode and cathode.
Connect anode to the plus plug and cathode to the minus, then the LED should light up.
Anode is a bit longer than cathode.

Connect anode to obniz io0 and cathode to io1.

If you are using LED that is without resistor, you should add a resistor.

Turn ON LED

Open your obniz ProgramPage.

Let's first try to just turn on the LED.
Below is the code, which you can copy/paste it onto your program.

import asyncio
from obniz import Obniz

async def onconnect(obniz):
    obniz.io0.output(True)
    obniz.io1.output(False)

obniz = Obniz('OBNIZ_ID_HERE')
obniz.onconnect = onconnect

asyncio.get_event_loop().run_forever()

And run this program.

python led.py

LED then should get turned ON!

Explanation of program

Look at top of the program.

import asyncio
from obniz import Obniz

...

asyncio.get_event_loop().run_forever()

Obniz use asyncio module.

The first line teaches the program to use the asyncio module, and the last line starts the program using asyncio.
The second line teaches the program to use the necessary modules for various operations of obniz.
So, when working with obniz, these three lines are always required. Don't forget it when writing yourself.

Next, look at async def.

async def onconnect(obniz):
    obniz.io0.output(True)
    obniz.io1.output(False)

This create onconnect function which call when obniz connect.
Let's look at the contents of the process.

obniz.io0.output(True)

This shows that io0 of obniz is set as a positive pole.

obniz.io1.output(False)

And this line shows that io1 of obniz is set as a minus pole.
How did you decide this? Recall about the LED's anode and cathode, as described at the beginning of this page.
The LED was lit by connecting positive to the anode and negative to the cathode. So you have set io0 connected with the anode positive and set io1 connected the cathode negative.

obniz = Obniz('OBNIZ_ID_HERE')

Obniz('OBNIZ_ID_HERE')creates an object representing obniz corresponding to obniz id and assigns it to obniz.

obniz.onconnect = onconnect

By assigning the name of the function you defined to obniz.onconnect, you can set the function to be executed when obniz completes connection.
Since we previously defined the function onconnect, we putonconnect here.

Control LED

Next, let's make ON and OFF buttons on HTML, and control LED using these buttons.
To change ON/OFF, it needs switch. Use obniz embedded switch.

Below is the code, which you can copy/paste it onto your program.

import asyncio
from obniz import Obniz

async def onconnect(obniz):
    def read_state(state):
        if state == "push":
            obniz.io0.output(True)
            obniz.io1.output(False)
        else:
            obniz.io0.output(False)
            obniz.io1.output(False)
    obniz.switch.onchange = read_state

obniz = Obniz('OBNIZ_ID_HERE')
obniz.onconnect = onconnect

asyncio.get_event_loop().run_forever()

The LED should light up by pressing the switch on the obniz!

Explanation of program

Let's take a closer look at this program. I will omit the same part as the previous program.
Let's focus on the part of function definition that starts with async def. This time also defines a function named onconnect.
First, look at the last line of the function definition.

obniz.switch.onchange = onconnect

obniz.switch.onchange represents a function to be called when the state of the switch attached to obniz changes.
This line tells you that if the switch's state changes, execute the read_state function.
The function executed by obniz.switch.onchange is passed the switch state as an argument. The switch status is

  1. none (no operation is done)
  2. left (operation to rotate left)
  3. right (operation to rotate to the right)
  4. push (in the state of being pushed in)

There are four types of
Let's turn on the LED when it is pushed this time.

def read_state(state):
    if state == "push":
        obniz.io0.output(True)
        obniz.io1.output(False)
    else:
        obniz.io0.output(False)
        obniz.io1.output(False)

As mentioned earlier, this function is executed by passing the switch state when the switch state changes, so state contains one of the four switch states.
In order to turn on when the switch is pressed, conditional branching is performed by the if statement.

if state == "push":

This is only executed when the switch status is " push ". These two lines are the process to turn on the LED that I saw in the previous program.

else:

Those two lines are executed when the switch state is other than " push ". In these two lines, both arguments are False. By doing this, you can turn off the LED.

This completes the description of the program to turn on the LED with obniz.
You could easily get the light from the program!





You will Get in Few Days

Circuit for Starter “obniz Board” is available on Amazon and other online stores.
You can get it at below

Our products and resellers

Forum

Visit our developer’s forum to discuss and discover technologies.

Forum

Contact

Feel free to contact out support and technical team.

Contact us