Table of Contents

How to Create a Toggle Switch Logic for Arduino using PictoBlox

Arduino Toggle LED State
Example Description
Learn how to create a simple Arduino project in PictoBlox where pressing a push button toggles an LED ON and OFF. Every time the button is pressed, the LED changes its current state—if it's ON, it turns OFF, and if it's OFF, it turns ON.

Introduction

Push buttons are incredibly popular electronic components found in everything from your TV remote to gaming controllers! Learn what a push button is, how state-toggling logic works, how to interface an LED and a push button with Arduino, and program them with ease in PictoBlox – our Scratch blocks-based graphical programming platform with advanced hardware interaction abilities. Once you’re equipped with the basic knowledge of tracking variable states and hardware debouncing, we’ll show you how to build a smart light switch and other exciting DIY projects using the push button.

To work in PictoBlox, you’ll first need to download it from HERE.

Ready? Set. Go!

Prerequisites

  • Arduino Uno R3 Board
  • 220Ω Resistor
  • Breadboard
  • LED
  • Laptop or Computer
  • PictoBlox installed on the system
  • Jumper Wires
  • Push Button

How to Connect with Arduino Uno

  1. Connect Uno to your computer and open PictoBlox.
  2. In PictoBlox, go to the toolbar and click on the Board menu. Select Arduino Uno.
  3. Next, click on the Connect menu, select the Port to which Arduino Uno is connected e.g. COMXX or ttyXX. Once you select the port, the icon beside the Connect tab will become connected.Port Connection
  4. On the top right corner, there is a toggle button named Mode. This button is used to switch between the two working modes of PictoBlox, namely the stage mode and the upload mode.
  5. Switch to Upload mode by toggling this button. You’ll observe some changes in the UI.

Connection

Step-by-Step Block Coding Guide

  1. Go to the Arduino Uno palette and drag the ‘when arduino starts up’ block into the scripting area.
  2. Create Two Variables from the Variables palette. Label one as Switch pressed’ and the other as Switch State’. Make sure both of the Variables are of number type.
  3. From the Variables palette, add the set ( ) to ( )’ block. Use the ‘Switch pressed’ variable and set its value to “0”.
  4. From the Variables palette, add the set ( ) to ( )’ block. Use the ‘Switch state’ variable and set its value to “0”.Initialize Arduino Uno and Variables
  5. Create a custom block from the My Blocks palette. Label it as Toggle Switch’.
  6. A new define Toggle Switch’ will appear in your script area. From Control palette, add the “if () then else” block underneath it.
  7. From the Operators palette, add the “() = ()” block inside the if condition. From the Variables Palette, add the “Switch state” block in the first operand. For the second operand, use “0“.
  8. Finish the define Toggle Switch’ block as follows:Toggle State Definition
  9. Finish the earlier script as follows:Final Code - Arduino LED Brightness Control

To upload the code, click on the Upload Code button.Uploading

Output

Final Output - Arduino Toggle LED State

Conclusion

In this lesson, we explored how to create a toggle system using an Arduino Uno, a push button, and PictoBlox. We learned how to write conditional logic that detects a button press to switch and hold the state of an LED, moving beyond a simple momentary switch to a functional, real-time toggle interface. By interacting with the digital pins, we visualized how physical inputs can reliably control hardware states. With this fundamental concept mastered, you can now apply state-toggling logic to control more advanced actuators, switches, and components in your future DIY automation projects!