Library
Courses
micro:bit Coding Cards Kickstart
Code a micro:bit as a Light Switch
Cover image
Activity
Preview

Code a micro:bit as a Light Switch

Toggle as an on and off light switch.

Topics
Coding
Get Started
  • CSTA
    Grades: 3rd, 4th, 5th
    Algorithms & Programming, Algorithms
    Compare and refine multiple algorithms for the same task and determine which is the most appropriate.
    Grades: 3rd, 4th, 5th
    Algorithms & Programming, Control
    Create programs that include sequences, events, loops, and conditionals.
    Grades: 3rd, 4th, 5th
    Algorithms & Programming, Modularity
    Modify, remix, or incorporate portions of an existing program into one's own work, to develop something new or add more advanced features.
    Grades: 3rd, 4th, 5th
    Algorithms & Programming, Program Development
    Test and debug (identify and fix errors) a program or algorithm to ensure it runs as intended.
    Grades: 6th, 7th, 8th
    Algorithms & Programming, Control
    Design and iteratively develop programs that combine control structures, including nested loops and compound conditionals.
    Grades: 6th, 7th, 8th
    Algorithms & Programming, Modularity
    Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
    Grades: 6th, 7th, 8th
    Algorithms & Programming, Modularity
    Create procedures with parameters to organize code and make it easier to reuse.
  • National Curriculum
    Grades: Year 3, Year 4, Year 5, Year 6
    Design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.
    Grades: Year 3, Year 4, Year 5, Year 6
    Use sequence, selection, and repetition in programs; work with variables and various forms of input and output.
    Grades: Year 3, Year 4, Year 5, Year 6
    Use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs.
  • Curriculum for Excellence
    Grades: P5, P6, P7
    Computing Science, Understanding the world through computational thinking
    I understand the operation of a process and its outcome. I can structure related items of information.
    Grades: P5, P6, P7
    Computing Science, Understanding and analysing computing technology
    I can explain core programming language concepts in appropriate technical language.
    Grades: P5, P6, P7
    Computing Science, Understanding and analysing computing technology
    I understand how information is stored and how key components of computing technology connect and interact through networks.
    Grades: P5, P6, P7
    Computing Science, Understanding the world through computational thinking
    I understand the operation of a process and its outcome. I can structure related items of information.
  • Florida - NGSSS
    Grades: 3rd, 4th, 5th
    Communication Systems and Computing, Modeling and simulations
    Describe how models and simulations can be used to solve real-world issues in science and engineering.
    Grades: 3rd, 4th, 5th
    Communication and Collaboration, Communication and collaboration
    Identify ways that technology can foster teamwork, and collaboration can support problem solving and innovation.
    Grades: 3rd, 4th, 5th
    Communication Systems and Computing, Modeling and simulations
    Create a simple model of a system (e.g., flower or solar system) and explain what the model shows and does not show.
    Grades: 3rd, 4th, 5th
    Communication Systems and Computing, Human – Computer interactions and Artificial Intelligence
    Explain that computers model intelligent behavior (as found in robotics, speech and language recognition, and computer animation).
    Grades: 3rd, 4th, 5th
    Computer Practices and Programming, Computer programming basics
    Create, test, and modify a program in a graphical environment (e.g., block-based visual programming language), individually and collaboratively.
    Grades: 3rd, 4th, 5th
    Computer Practices and Programming, Computer programming basics
    Detect and correct program errors, including those involving arithmetic operators, conditionals, and repetition, using interactive debugging.
    Grades: 6th, 7th, 8th
    Communication Systems and Computing, Problem solving and Algorithms
    Decompose a problem and create a function for one of its parts at a time (e.g., video game, robot obstacle course, making dinner), individually and collaboratively.
    Grades: 6th, 7th, 8th
    Computer Practices and Programming, Programming applications
    Select appropriate tools and technology resources to accomplish a variety of tasks and solve problems.
    Grades: 6th, 7th, 8th
    Communication Systems and Computing, Hardware and software
    Identify and describe the use of sensors, actuators, and control systems in an embodied system (e.g., a robot, an e-textile, installation art, and a smart room).
    Grades: 6th, 7th, 8th
    Communication Systems and Computing, Human – Computer interactions and Artificial Intelligence
    Design and demonstrate the use of a device (e.g., robot, e-textile) to accomplish a task, individually and collaboratively.
Age range
8-14+
Duration
40:00
Assets
micro:bit Coding Cards

Code

Light Switch

00:10

Use a toggle variable to store the state of the RGB LED A. 1 means on, and 0 means off.

When the Button A is pressed, use an if statement to check the value of the variable and then, depending on the value, turn the LED on or off, and toggle the value stored inside the variable.

let toggle = 0
input.onButtonPressed(Button.A, function () {
    if (toggle == 0) {
        sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 100, 0, 0)
        toggle = 1
    } else {
        sb.setRgbLedColorRGB(sb.rgbLed(SBRgbLed.RgbLedA), 0, 0, 0)
        toggle = 0
    }
})

If using micro:bit for the first time, follow this setup.