LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > Circuits on the cyber:bot

Circuits on the cyber:bot

What it’s about

Learn how to build simple circuits on the cyber:bot robot’s breadboard, and then create block programs to interact with them. Add LEDs with resistors, and make the LEDs blink. Then add a pushbutton to control the LED, with and without programming.

Before you start

These activities are written assuming you are completing the cyber:bot Prerequisite tutorials first, and then doing the Main tutorials in sequence. For this tutorial you will need:

  • Your programming software set up, with your micro:bit ready to use the cyberbot extension
  • A fully built cyber:bot robot
  • Resistors, LEDs, and pushbuttons from your robot kit’s Small Robot Electronics Pack

After you finish

When you are done with these activities, you will understand how to connect electrical components on a solderless breadboard. You will be prepared to build sensor circuits for your cyber:bot that will allow it to roam around on its own.

Blink a Light

The cyber:bot board has a built-in Propeller microcontroller on its underside. The Propeller has 32 digital input/output pins, called I/O pins for short, that are designed to interact with circuits. These are referred to by number, P0 through P31.

Some of the I/O pins are connected to circuits built into the cyber:bot board, that your robot is already using. For example, P16 through P19 are connected to 3-pin headers, where the servo motors are plugged into P18 and P19. In this activity, we will use P20 and P21, which are connected to tiny built-in LED lights. Later on, we will build circuits on the breadboard, and connect them to the sockets alongside labeled P0–P15. Projects sent to the micro:bit module can instruct the Propeller to interact with these built-in circuits.

Hardware Setup

  • Set the cyber:bot board’s power (PWR) switch to Position 0.
  • Make sure the battery holder is loaded with 5 AA batteries.
  • Make sure the battery holder’s barrel plug is firmly plugged into the cyber:bot board’s barrel jack. 
  • Connect your micro:bit module to your computer with a USB cable.

Software Setup

  • In a browser, go to makecode.microbit.org to open the micro:bit Makecode Editor.
  • Make sure the cyberbot extension is added to the Project Files.
    (See Add extensions to your micro:bit).

Blinking Lights

We’ll experiment with the “O” (output) feature of an I/O pin with a program to turn the built-in LED circuit on and off.

  • Locate the small LED light.

On the cyber:bot, it’s a small part just above the P20 label in the lower-right corner of your board.

Example project: pin_20_blink

  • Set the name to pin_20_blink and enter the project below.
    (See Save & Edit Projects and Flash Projects with MakeCode Editor.)
  • Click Download.
    (See Flash Projects with MakeCode Editor.)
  • Set the cyber:bot board’s PWR switch to 1.
  • Watch the LED above the P20 label. It should blink slowly, once every 3 seconds.

How it works

By using the cyber:bot extensions write digital block instead of the already included digital write block, the command is then sent to the Propeller microcontroller instead of being processed by the micro:bit. From there, the Propeller uses its pre-programmed firmware to interact with circuits connected to its I/O pins.

The block (Pin20) write digital (High) sets Propeller I/O pin P20 to “output high” which means it connects to its 3.3 V supply, as shown on the left side of the image below. The pin applies 3.3 V of electrical pressure to the LED circuit, causing electric current to pass through it and the light to turn on. After that, pause (2000) makes the program do nothing for 2000 ms, which keeps the light on for 2 seconds.

Next, (Pin20) write digital (Low). This sets P20 to output-low, which connects the pin to its 0 V ground supply voltage instead, as shown on the right side of the figure below. This takes away the electrical pressure, so the current stops flowing through the circuit and the light turns off. Another pause (1000) makes the light stay off for one second.

Those four commands are in a forever block loop, which repeats endlessly, so the light keeps blinking.

Try this: pin_20_blink_fast

You can change the light’s blink rate by changing the pause block’s ms argument. For example, to make the light blink a lot faster, significantly reduce the pause time. 

  • Rename the project pin_20_blink to pin_20_blink_fast or make a new one
  • Change pause (2000) and pause (1000) to pause (50).
  • Flash the project, then watch the LED located at P20 — is it blinking faster now?

Your turn

Expand your project to control the P21 light along with the P20 light.

  • Modify your program so that it turns both lights on and off at about the same time.
  • Modify your program so that whenever one light is on, the other is off.

Get Ready to Build

Building and controlling circuits with the cyber:bot board is a great way to learn about electricity, and to experiment with making your own inventions. Building experimental circuits to design your own projects is called prototyping, and it is a real-world engineering skill.

Before you start your cyber:bot prototyping, there are three important Reference pages you should have handy, especially if you have never built circuits on a breadboard before. Just click on each link and the page will open in a new tab or window.

  • Open the Breadboard Basics page, and watch the video if you can.
  • Open the Schematic Symbols page, and look up the symbols for resistor, light-emitting diode, pushbutton, and microcontroller input and output pins.
  • Open the Resistor Color Codes page (we’ll use it soon).
  • Now, keep reading below to learn about the breadboard on the cyber:bot.

The Breadboard on the cyber:bot

The cyber:bot Board has a solderless breadboard mounted on it. The breadboard lets you connect common electronic components together to build your own circuits.

The cyber:bot’s breadboard is surrounded on three sides by black sockets. These make it convenient to connect circuits on the breadboard to power, ground, and the Propeller I/O pins. There are also sockets to connect to a digital-to-analog converter signal from the Propeller chip, and to an analog-to-digital converter on the micro:bit.

Remember the 3-Position Switch Guidelines!

Before you build circuits on the breadboard, let’s review:

  • Position 0: ALWAYS use 0 for building or modifying circuits; also good for flashing projects. Position 0 turns off power to the whole cyber:bot board. If the micro:bit is plugged into USB, you can still flash and run non-robot projects.
  • Position 1: Good for flashing projects and powering breadboard circuits. Position 1 powers the Propeller system and sockets along the breadboard, but not the 3-pin/Servo ports, so your cyber:bot won’t roll away the moment you program it. Position 1 also powers the micro:bit independent of its USB port. 
  • Position 2: ONLY use 2 when you are ready for your robot to move. NOT good for flashing projects. Position 2 powers all the circuits on the board, including the 3-pin servo ports, and also the micro:bit independent of its USB port.

Build LED Circuits

You have blinked the built-in LEDs on your board. Now it’s time to build your own LED circuits on your cyber:bot board’s prototyping area.

Introducing the LED

A diode is an electrical part that only lets electricity flow through it in one direction. A light-emitting diode (LED) emits light when current passes through it. You have to make sure to connect an LED the right way for it to light up. If you plug an LED in backwards it will not hurt it, but it will not emit light.

An LED has two terminals: the anode and the cathode. In the picture below, the anode lead is labeled with the plus-sign (+), and it is the wide part of the triangle in the schematic symbol. The cathode lead is labeled with a minus-sign (-), and it is the line across the point of the triangle in the schematic symbol.

You can tell the anode and cathode apart by the shape of the LED’s plastic case. Look closely — it’s mostly round, but there is a small flat area near the cathode lead. Also note that the LED’s leads are different lengths. Usually, the shorter lead is connected to the cathode.

Always check the LED’s plastic case. Sometimes the leads have been clipped to the same length, or a manufacturer does not follow this convention.

Example LED Circuits

Let’s build two LED circuits on your breadboard. In addition to the two LEDs, you will need two resistors. Unlike the LEDs, the resistors do not have positive and negative leads, so you don’t have to worry about plugging them in backward. Resistors resist the flow of electrical current. Each one has a value that tells how strongly it resists current flow, measured in ohms, often noted by Greek letter omega: Ω. Take a look at that Resistor Color Codes page to see how its bands indicate its value.

Parts Needed
(2) red LEDs
(2) 220 ohm resistors (red-red-brown)
(misc) Jumper wires

  • Put the cyber:board power swtich in position 0.
  • Build two LED circuits, one each connected to P9 and P8, according to the schematic.

  • Use the wiring diagram for your board, below, to double-check your work.

LED Test Project: pin_8_blink

This is the same test code that was used to blink the built-in P20 LED earlier in this tutorial, with the I/O pin number updated.
Enter and flash the project pin_8_blink.

  • Move the power switch to Position 1, to power the breadboard circuits. The LED connected to P8 should start blinking.
  • If it is not, check your wiring and try again. You might have the LED in backwards, or one of the leads or wires in the wrong socket.
  • Once you have the P8 LED blinking, change Pin8 to Pin9, then re-run the code to test your P9 LED circuit.
  • If you’ve verified that both your LED circuits are working, great! Continue on!

Your Turn

  • Try changing the pause times for different effects.
  • Try adding a pause call after the for loop.
  • Try duplicating the for loop, and pasting it after that pause call. Then reverse its range from 9 to 7.
  • Try modifying the program to make both of the LEDs blink at the same time.

Check Pushbuttons

Many items you use every day have pushbuttons. Cell phones, microwave ovens, TV remotes, and computer keyboards might all have pushbuttons. Can you think of others?

Let’s use a common pushbutton circuit and build a program for monitoring it with your microcontroller. Then, let’s use a pushbutton’s state to control LED circuits. LEDs are just one example of a device you can turn on and off with a microcontroller. Your invention might instead use pushbuttons to control circuits for motors, heating elements, or other devices.

The micro:bit module has two built-in pushbuttons. You can also add more pushbuttons to your cyber:bot on the breadboard. Doing so, you will learn how a pushbutton circuit works.

About Pushbuttons

A pushbutton is a device that makes an electrical connection between two of its terminal leads when its button is pressed. When the button is released into its normally-open state, the electrical connection is broken and no current flows through the device. Here is the schematic symbol:

…and here is a drawing that resembles a breadboard-friendly pushbutton:

Notice that when the button is pushed, all 4 pins are connected. However, when the button is not pushed, legs 1 and 4 are still connected and legs 2 and 3 are still connected.

If the pushbutton is pressed, the circuit applies 3.3 V to the I/O pin through the pushbutton, and a small amount of current also passes through the 10 kΩ resistor to ground. When the pushbutton is not pressed, the connection to the 3.3 V supply is broken, and so the circuit applies GND (0 V) to the I/O pin.

Pushbutton and LED Circuits

This circuit uses your board’s built-in P20 and P21 LEDs, along with two pushbutton circuits you will build onto your breadboard. Use 220 ohm resistors to connect the pushbutton circuits to the cyber:bot I/O pins and use 10 k-ohm resistors to connect the circuits to ground.

Parts
(2) pushbuttons
(2) 220 ohm resistors (red-red-brown)
(2) 10 k-ohm resistors (brown-black-orange)

  • Build the two pushbutton circuits shown in the schematic and wiring diagram. We are also using the built-in LEDs on P20 and P21.

Test project: test_pin_3_button

This test project will display the state of the button connected to P3 on the micro:bit module’s display. The display will show a 1 if the button is pressed, or 0 if it is not pressed.

  • Enter and flash the project test_pin_3_button.

How test_pin_3_button Works

This Block code is all inside of a continuous forever loop. Inside the loop there is one if statement that checks two conditions. 

The first condition that it checks is whether the (Pin3) read digital block is equal to 0. If this is true the micro:bit module will display a 0. The read digital block checks the input state of the pin, in this case P3. Thus, if the button is not being pressed, the input state at pin 3 is 0. 

The second part of the if statement checks to see if the input state is 1 (button pushed), if so the micro:bit module will display a 1.

Did You Know?

Active-high: The pushbutton circuit you are using is called active-high because the pushbutton sends the cyber:bot I/O pin a 3.3 V high signal when pressed (active) or a 0 V low signal when released.

Pull-down: The 10 k-ohm resistor in the schematic is called a pull-down resistor. It’s there so that the I/O pin will detect 3.3 V through the resistor when the button is pressed. If you leave it out, the I/O pin behaves more like an antenna, and nearby electric fields will end up controlling whether 1 or 0 is detected.

Active-low, pull-up: Pushbutton circuits can also be active-low. All you have to do is swap the pushbutton and 10 k-ohm resistor in the schematic. The pull-down resistor then becomes a pull-up resistor, and the I/O pin will detect 0 V when the button is pressed.

Try This: pin_3_button_LED

Now it is time to control an LED with a pushbutton.

  • Change the show number blocks to the write digital block to turn on the yellow LEDs attached to pin 20 when the button is being pressed.
  • Change test_pin_3_button to pin_3_button_LED or make a new one
  • Change display show (0) to (Pin20) write digital (Low) and change display show (1) to (Pin20) write digital (High).
  • Enter and flash the project below:

  • Verify that the P20 LED blinks if you press the pushbutton connected to P3, and turns off when you release it.

Your Turn – P21 LED and P4 Pushbutton

Can you make the LED connected to P21 light when the button connected to P4 is pressed?

  • Modify your Try This project to test controlling the P21 LED with the P4 pushbutton.
  • Expand the code so that the P3 button controls the P20 LED and the P4 button controls the P21 LED at the same time.

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024


Source URL:https://learn.parallax.com/courses/circuits-on-the-cyberbot_makecode/
Links