Turn on/off the LED with the button on the web page

Make things

The LED connected to obniz can be turned on and off by the button on the web page.

Materials

  • LED
  • (If you use an LED that does not have a built-in resistor, use a resistor.)
  • obniz Board
  • Mobile batteries, etc.
  • Device that opens the browser

How to make

Hardware connection

Connect obniz Board and LED with reference to the LED parts library as shown in the figure below.

obniz LED
0 Anode (longer)
1 Cathode (shorter)

In the case of resistive LEDs

 

In case of LED without resistance

※When using an LED that does not have a built-in resistor, be sure to put a resistor between it and the LED as shown in the figure.

 

Software

Changes the disabled attribute of the button element when the button is pressed.
When the ON button is pressed, the ON button is disabled and only the OFF button is enabled.

$('#on').click((e) => { led.on(); $('#on').prop('disabled', true); $('#off').prop('disabled', false); });
$('#off').click((e) => { led.off(); $('#on').prop('disabled', false); $('#off').prop('disabled', true); });

Program