🔬
Wiki.Linhkientot
  • 👋Chào mừng tới Linhkientot's Wiki
  • Hướng dẫn phần mềm
    • Cài đặt Arduino IDE 2.0
    • Cài đặt phần mềm Mixly
    • Cài đặt phần mềm Mind+
    • Cài đặt phần mềm Thonny
  • Arduino Products
    • Hướng dẫn ban đầu Arduino IDE
      • Hướng dẫn nạp chương trình Arduino IDE
      • Thêm thư viện Arduino IDE
      • Cài đặt Driver CH340 trên MAC OS
      • Lập trình Board ESP32 trên Arduino IDE
    • Arduino Starter Kit
      • Lesson 0. Getting Started - install IDE
      • Lesson 2. LEDs
      • Lesson 3. RGB LEDs
      • Lesson 4. Eight LEDs and a Shift Register
      • Lesson 5. The Serial Monitor
      • Lesson 6. Digital Inputs
      • Lesson 7. Make an RGB LED Fader
      • Lesson 8. Analog Inputs
      • Lesson 9. Sensing Light
      • Lesson 10 Making Sounds
      • Lesson 11 LCD Displays Part 1
      • Lesson 12 LCD Displays Part 2
      • Lesson 13 DC Motors
      • Lesson 14 Servo Motors
      • Lesson 15 DC Motor Reversing
      • Lesson 16 Stepper Motors
      • Lesson 17 Email Sending Movement Detector
    • Arm Robot
      • Hướng dẫn lắp ráp Arm-4DoF
      • Hướng dẫn lắp ráp Arm-6DoF
      • Hướng dẫn lắp ráp Arm - 6DoF đế tròn quay
    • Car Robot
      • Hướng dẫn lắp ráp tank TS-100
    • Arm Car Robot
    • Smart home IoT
    • Lập trình cảm biến khí ga/ khói MQ-2 Arduino
  • Micro:bit Products
    • Micro:bit Setup
    • Micro:bit Starter Kit V1
      • Setting up the micro:bit with Makecode
      • Set up Arduino IDE for micro:bit
      • Using the Buttons and LED Matrix on micro:bit
      • Pushbutton with micro:bit
      • Tilt Sensor with micro:bit
      • Temperature Sensor with micro:bit
      • DC Motor with micro:bit
      • Sound Sensor with micro:bit
      • Raindrop Sensor with micro:bit
      • Make an RGB LED Blink with micro:bit
      • Ultrasonic Distance Sensor with micro:bit
      • Analog Inputs and micro:bit
      • Servo with micro:bit
      • Smoke sensor with micro:bit
      • Light-dependent Resistor with micro:bit
      • Infrared Obstacle Avoidance Sensor with micro:bit
      • Using LEDs with micro:bit
    • Micro:bit Starter Kit V2
      • Microbit Introduction
      • Microbit Basic Lessons
      • 1. Lesson: Đèn LED nhấp nháy
      • 2. Lesson: RGB Led
      • 3. Lesson: Đọc giá trị từ triết áp
      • 4. Lesson: Đọc nút nhấn
      • 5. Lesson: Servo Motor
      • 6. Lesson: Passive Buzzer-Còi thụ động
      • 7. Lesson: Active Buzzer-Còi chủ động
      • 8. Lesson: Compass-La bàn
      • 9. Lesson: Accelerometer-Gia tốc kế
      • 10. Lesson: Module phát hiện âm thanh
      • 11. Lesson: Cảm biến ánh sáng (quang trở)
      • 12. Lesson: Cảm biến ngọn lửa
      • 13. Lesson: Cảm biến khói
      • 14. Lesson: Cảm biến siêu âm
      • 15. Lesson: ModuleRelay
      • 16. Lesson: Cảm biến nhiệt độ (DHT11)
      • 17. Lesson: Bộ điều khiển từ xa (IR)
      • 18. Lesson: Hiển thị màn hình LCD i2c 1602
      • 19. Lesson: Hiển thị nhiệt độ, độ ẩm trên màn hình LCD
      • 20. Lesson: Điều khiển quạt bằng Relay
    • Micro:bit Advanced Kit V2
      • Giới thiệu BBC Sensor Shield V2
      • Danh sách linh kiện bộ Kit
    • Micro:bit Car
  • ESP32 PRODUCTS
    • Hướng dẫn lập trình ESP32
  • Raspberry Products
    • Raspberry Pico Kit
    • Raspberry Pico Car
  • Group Robots
    • 🤖Arduino Robots
    • 🐦Micro:bit Robots
    • 🍓Raspberry Robots
Powered by GitBook
On this page
  1. Micro:bit Products
  2. Micro:bit Starter Kit V1

Using LEDs with micro:bit

PreviousInfrared Obstacle Avoidance Sensor with micro:bitNextMicro:bit Starter Kit V2

Learn to use multiple LEDs with the micro:bit and make a light show

Written By: Cherie Tan

Introduction

A light-emitting diode (LED) is a semiconductor device that emits light when an electric current passes through it.

In this guide, you will learn to connect external LEDs to the micro:bit, and create a light show.

Complete this guide to learn how to use external LEDs with the micro:bit!

Step 1 LED

  • Before we begin, let's take a look at the external LED.

    The LED has two leads, a negative lead and a positive lead.

    The longer leg is the positive lead, also called the anode.

    The shorter leg is the negative lead, also called the cathode. This will be connected to GND.

  • If the legs are trimmed, you can also tell by looking at the flat edge on the LED's outer casing. The pin closest to the flat edge will be the negative pin, the cathode pin.

Step 2 Connect Cathode to GND

Step 3 Add a resistor

Step 4 Connect Anode to P2

Step 5 MakeCode

basic.forever(function () {
    pins.digitalWritePin(DigitalPin.P2, 0)
    basic.pause(500)
    pins.digitalWritePin(DigitalPin.P2, 1)
    basic.pause(500)
})
  • Copy and paste this code to the Javascript interface.

  • Programming the external LED is similar to how it was done for the Two Colour LED module. We've used 'pins.digitalWritePin()' to turn the LED on and off.

Step 6 MakeCode for Light Show

led.enable(false)
basic.forever(function () {
    pins.digitalWritePin(DigitalPin.P0, 1)
    basic.pause(1000)
    pins.digitalWritePin(DigitalPin.P1, 1)
    basic.pause(1000)
    pins.digitalWritePin(DigitalPin.P2, 1)
    basic.pause(1000)
    pins.digitalWritePin(DigitalPin.P3, 1)
    basic.pause(1000)
    pins.digitalWritePin(DigitalPin.P4, 1)
    basic.pause(1000)
    pins.digitalWritePin(DigitalPin.P4, 0)
    basic.pause(1000)
    pins.digitalWritePin(DigitalPin.P3, 0)
    basic.pause(1000)
    pins.digitalWritePin(DigitalPin.P2, 0)
    basic.pause(1000)
    pins.digitalWritePin(DigitalPin.P1, 0)
    basic.pause(1000)
    pins.digitalWritePin(DigitalPin.P0, 0)
    basic.pause(1000)
})
  • Copy and paste this code into the Javascript interface.

Step 7 Arduino Code for Light Show

const int lowestPin = 0;//the lowest one attach to pin 0
const int highestPin = 4;//the highest one attach to pin 4


void setup()
{
  //set pins 0 through 4 as output
  for(int thisPin = lowestPin;thisPin <= highestPin;thisPin++)
  {
    pinMode(thisPin,OUTPUT); //initialize thisPin as an output
  }
}

void loop()
{
    //iterate over the pins
    //turn the led on from lowest to the highest
    for(int thisPin = lowestPin;thisPin <= highestPin;thisPin++)
    {
      digitalWrite(thisPin,HIGH);//turn this led on
      delay(1000);//wait for 1 second
    }
    //fade from the highest to the lowest
    for(int thisPin = highestPin;thisPin>=lowestPin;thisPin--)
    {
      digitalWrite(thisPin,LOW);//turn this led off
      delay(1000);//wait for 1 second
    }
  }
  • Optionally, you may want to use the micro:bit with the Arduino IDE. Open the Arduino IDE. If you have not got it installed, please follow our previous guide on how to install and set up the Arduino IDE for micro:bit. It requires a few more steps than just installing the IDE, to get it working with the micro:bit, so please follow that guide.

  • Then, copy and paste this code to the Arduino IDE

Step 8 Upload the code

  • It's time to upload the code to the micro:bit. In the Arduino IDE, click on the tick icon on the top left-hand corner to verify the code.

  • Then click on the upload button next to it.

  • Wait for the code to finish uploading. You will know it is done when it states 'Done uploading' on the bottom left-hand corner.

Step 9 Using multiple light-emitting diodes

  • To use more than one external LED, follow these steps. We'll use multiple LEDs with the micro:bit to create a light show. So go on and add a four more LEDs as shown in the diagram.

  • We'll use a total of five LEDs to create a light show by programming the micro:bit using the MakeCode editor, and then with the Arduino IDE.

  • Each of the cathode (negatively charged lead) of the LEDs needs to be connected to the negative rail.

  • Each of the anode (positively charged lead) of the LEDs need to be connected to P0 to P4, with the use of a resistor.

  • Remember to connect the negative rail to a GND pin.

Upload it to the micro:bit and you should see the LEDs lighting up one after another and then going off. Pins 3 and 4 GPIO are shared with the micro:bit's LED screen. To use Pins 3 and 4 for the light show, we will first disable the LED screen by using the '' block on start

led enable false