Lesson video

In progress...

Loading...

Hello, my name is Mrs. Holbrook and welcome to Computing.

I'm so pleased you've decided to join me for the lesson today.

In today's lesson, we're going to be adding lighting to our robot buggy and we're going to be synchronising components and systems. Welcome to today's lesson from the unit using physical computing to create a robot buggy.

This lesson is called Creating an Autonomous Vehicle, and by the end of today's lesson, you'll be able to synchronise components and systems. Shall we make a stat? We will be exploring these key words throughout today's lesson.

Let's take a look at them now, vehicle.

Vehicle, a machine used for transporting people or goods.

Autonomous.

Autonomous, something that operates independently without human intervention.

Synchronise, synchronise when two or more things work together in a certain way.

Look out for these keywords throughout today's lesson.

Today's lesson is broken down into two sections.

We'll start by adding lighting to a buggy.

We'll then move on to synchronise components and systems. Let's make a start by adding lighting to a buggy.

An LED is a small electronic component that emits light when electricity passes through it.

LED is short for light emitting diode.

LEDs use very little power and can come in many colours.

So you can see here I have four LEDs, all different colours, a yellow, a blue, a red and a green.

LEDs are used in a wide variety of places because they are small, efficient, and reliable sources of light.

LEDs are used around the home in lighting and products such as TVs, computer screens, and remote controls.

LEDs are used in vehicle headlights, brake lights, indicator lights, and dashboard and warning lights.

Izzy says, "Hey Alex, I think it might be cool to add LEDs to the project we've been working on." Alex says, "That's a great idea, Izzy, we could add headlights and brake lights to the buggy vehicle." This sounds like this could be a great idea.

Well done Izzy and Alex.

To use LEDs safely and effectively in projects, there are a few things that need to be considered.

LEDs must be connected into a circuit the correct way around.

LEDs are very sensitive and can be easily damaged if too much electrical current flows through them.

An LED usually has two connecting pins called the anode and cathode.

The anode is positive and the cathode is negative.

LEDs are designed to only allow electrical current to flow through them in one direction.

To connect an LED the correct way round in a circuit, you must be able to identify both pins.

So you'll notice from the diagram that the LED has two pins, but they're different lengths.

And if you look carefully here you can see my LED has two pins which are of different lengths.

The positive anode is the longer pin.

The negative cathode is the shorter pin.

Izzy says, "Hey Alex, did you know LEDs only let electrical current flow in one direction?" Alex says, "We need to remember that when we add LEDs to our project, Izzy." Resistors are used in circuits to reduce the flow of electrical current.

Certain components such as LEDs can break or have a reduced lifetime if too much current flows through them.

When using LEDs with a raspberry pi pico, you should use a resistor of around a hundred ohms. Any resistor from 75 to 220 ohms will work.

To protect the LED From too much current a resistor should be connected.

Your resistor can be connected to either leg of your LED and then to your raspberry pi pico.

A resistor value between 75 and 220 ohms should work well.

A resistor with brown black, brown colour bands has the value of a hundred ohms. To attach a resistor to an LED, you can bend one of the resistor legs around the positive leg of the LED.

So you can see here in the diagram the resistor has been connected to the leg of the LED.

You then twist the resistor leg around the LED leg about three times.

So you can see here, look, it's been curved around the leg around three times in the diagram.

A piece of electrical tape may help make it more secure, particularly if your LED is going to be moving about in a project.

Jumper wires can then be connected to the LED and resistor pins.

So you can see in this diagram the two jumper wires have been attached one to the resistor and one to the other leg of the LED.

You can then directly connect the other end of the jumper wire directly to the Pico or via a breadboard.

So in this diagram we are directly connecting the jumper wires to the Pico.

The positive pin connects to the free GPIO pin.

The negative pin connects to ground or GND.

You can repeat the process if multiple LEDs are needed in a project.

So you can see here this example has three LEDs connected to the Pico.

Note you may have to choose GPIO pins carefully if you are working with a larger project such as a buggy.

Izzy says, "I've connected the LEDs to the Pico, but how do we turn on the LEDs using code?" That's a really good question, Izzy, do you know the answer? Perhaps pause the video whilst you have a think.

LEDs are output components and can be controlled with code like other components you may have used already.

Once you have connected an LED, you can use some micro python test code to check that the LED works.

So let's have a look at this sample code together.

On line one we have from machine import pin.

So we are importing the machine module on line two we have LED is equal to pin.

Notice the capital P, (13, Pin.

OUT) This is creating the LED objects.

And then on line five we have led.

on.

This is going to turn on the LEDs.

You can make the LED flash by importing the U time module and using a while loop.

So we've extended the code a bit here.

So on line two we have import utime.

And then on line five we've changed our code to a while loop.

So we have wild true and then we have some indented lines of code starting on line six with LED on, to turn the LED on.

And then we've got the utime.

sleep for 0.

5 seconds and then we are turning the LED off and then we are having another sleep of 0.

5 seconds.

So this LED will turn on and off with a 0.

5 second pause in between each one.

So to recap, we are importing the utime module.

We are using a wire loop.

We are turning on the LED.

We are waiting for 0.

5 seconds.

We are turning off the LED and we're waiting for 0.

5 seconds again.

Time to check your understanding.

I have a question for you.

Which pin on an LED is positive? Is it A, anode.

B cathode or C diode.

Pause the video whilst you have a think about your answer.

Did you select A anode? Well done.

I have a true or false statement for you.

LEDs are designed to allow current to safely flow through them in both directions.

Is this true or false? Pause the video whilst you have a think.

If you select false, you'd have been correct.

LEDs are designed to only allow electrical current to flow through them in one direction.

Another true false statement, the shorter pin on an LED is the negative and is known as the cathode.

Is this true or false? Pause the video whilst you have a think.

Did you select true? Well done.

Okay, we're moving on to our first task of today's lesson Task A.

For part one if you can follow the instructions in task A activity one to add headlights to your buggy.

The instructions are provided as an additional material for today's lesson.

For part two, if you can follow the instructions in task A activity two to add brake lights to your buggy.

Pause the video here whilst you complete the tasks.

How did you get on? Did you manage to add some lights to your buggy? Great work.

Let's have a look at some sample answers together with some code and some images.

So you can see here we've got a photograph of the buggy which has some headlights which have been attached.

Our code says from machine import pin and then we have line three left_front_led.

So this is going to be the left headlight is equal to pin, (2, Pin.

OUT)and then line four very similar.

But this time we have called it right front LED and we're using pin three.

On line six and seven we are turning those LEDs on.

So we have left_front_led.

on().

Note you may have assigned different pin numbers to your LEDs depending on what pins you have available in your project.

For part two you are asked to add brake lights to your buggy.

So these are gonna go on the back of your buggy.

So very similar this time.

You can see with the code we've just added two more lines of code.

So we have left underscore rear underscore LED and we are assigning that to pin four and then we have the same but for the right rear LED, which has been assigned to pin five and then on lines 10 and 11, we are turning those LEDs on note.

Again, you may have had to assign different pin numbers to your LEDs depending on what pins you have available in your project.

If you didn't quite get your LEDs working correctly on your buggy, remember you can pause the video here, look carefully at the code and make any corrections you might need to.

Okay, we are now moving on to the second part of today's lesson where we're going to synchronise components and systems. Alex says, "I've got lots of wires, code and components in my buggy.

It's starting to look a bit confusing." As physical computing projects grow, they can start to seem complex and hard to manage.

Izzy says, "I agree Alex, how can we make our project easier to understand and manage?" You can keep your project manageable by using a few strategies such as keeping your project organised, testing as you go, and using modular code.

Alex has a question, "What's modular code?" Do you know what is meant by modular code? Maybe pause the video whilst do you have a think.

Modular code is when you write your code in small sections or blocks instead of a single large amount of code.

Each section or block of code is designed to do just one job.

Using modular code can make a large amount of code easier to understand and work with.

It also allows you to test, improve and reuse each block of code.

Functions can be used to help you create modular code.

A function is a reusable block of code that performs a specific task.

You can define a function in micro python using the deaf keyword.

So here we have a function called forward.

So we have the keyword deaf, then forward, open close brackets, and then a semicolon.

We then have the indented lines of code which are going to form that function.

So just to recap, the word deaf allows us to define a function.

Once defined, you can call the function to use it in your code whenever you need it and it can be called multiple times.

So you can see here we have a wild true loop and inside that loop we are calling the function forward and we just do that by writing the name of the function, which in this case is forward with open and closed brackets.

So the forward function has been defined and then the forward function is called.

Alex says, "I have a piece of code in my main loop that makes my LED blink." So here's Alex's code.

While true led.

on, utime.

sleep, 0.

5, led.

off, utime.

sleep, 0.

5.

Alex could extract the code outta the main loop and create a function with a suitable name.

Do you know what a suitable name might be for this function.

Maybe pause the video whilst you have a think.

Ah, Alex has gone for blink_led.

That's a suitable name.

Did you have something similar? Alex says that's better.

The code is easier to follow and I can reuse the function whenever I need to.

So the blink_led function is defined, and then the blink led function is called.

Time to check your understanding.

I have a question for you.

What key word is used to create a function in micro python? Is it A, if.

B, import.

Or C, def? Pause the video whilst you have a think.

Did you select C? Well done.

The table below shows some of the benefits of using modular code.

So it's easier to understand.

Code is broken down into smaller, simpler parts.

It's easier to debug.

You can test your code one piece at a time.

It's reusable.

You can copy and paste functions into new projects.

If you know they work correctly in one project then you can reuse it in another.

And it's easier to improve.

You can update one part of the project without breaking the others.

An autonomous buggy is likely to include several different systems. Components like motors, LEDs, and sensors or need to work at the right time.

The systems must work together in a way that the buggy performs as intended.

They must be synchronised.

Synchronisation means making sure all parts of your project act in a planned and logical sequence.

If components are run in the wrong order, your buggy may behave unexpectedly or fail to complete its task.

Modular code helps you to synchronise components by letting you control when each one runs in the main loop.

Alex says, "Izzy, do microcontrollers run all the programme task at the same time?" Do you know the answer to this question? Maybe pause the video whilst you have a think.

Izzy says, "I don't think so Alex, I think they complete one task, one after the other." What do you think? Do you agree with Izzy? A microcontroller reads code one liner at a time from top to bottom in a sequence that it is written.

Microcontrollers don't multitask.

They do each line in sequence from start to finish.

This is why the sequence of your code matters because the order of tasks changes the outcome.

You should note that there are many ways to synchronise the components and systems on a buggy.

Your code may look very different from someone else's even if the code completes the same objectives.

However you create your code, you should aim to make it easy to understand, debug and work with.

Time to check your understanding.

I have a true or false statement for you.

Synchronisation means making sure all parts of your project act in a planned and logical sequence.

Is this true or false? Pause the video whilst you have a think.

Did you select true? Well done.

I have a question for you.

Microcontrollers execute lines of code A, one at a time from top to bottom in the sequence it is written.

B, one at a time from bottom to top in the sequence it is written.

Or C, all at the same time to ensure synchronisation.

Pause the video whilst you think about your answer.

Did you select A? Well done.

Remember the sequence flows from top to bottom.

Okay, we are moving on to our next task of today's lesson, task B.

And you've done a fantastic job so far.

So well done.

If you can follow the instructions in task B activity three.

To write a programme that allows a buggy to complete a series of tasks autonomously, remember the instructions are provided as an additional resource for this lesson.

Pause the video here whilst you complete the task.

So for part A, when your programme starts, your buggy should flash its headlights three times and then wait for five seconds.

Let's have a look at the code that we've created to do this.

So on the left hand side we have our functions to control the headlights.

So we have a function for headlights on, which will turn both the left and right LED on.

And then we have a function that turns the headlights off, which does the opposite of the on function.

And then we have a third function which is flash_headlights and that's being passed a parameter of times here.

So we then have a for loop, which says for i in range times with a semicolon, we have head underscore lights_on.

We are then using the utime module.

So utime.

sleep 0.

5, we are turning the headlights back off and then we are sleeping for 0.

5 seconds.

On the right hand side, we are setting up the LED light pins.

So the actual pins that are going to be connected to the LEDs on the micro controller.

So the left front LED is going to be connected to pin two, the right front LED to pin three, the left rear LED to pin four, and the right rear LED to pin five.

And then we've got the code which is going to start the sequence for the buggy before running the main loop.

So we have brake lights on, we have flash headlights and we're passing it the value three.

So we want it to flash on and off three times.

We are then sleeping for five seconds, and then we are turning the brake lights off.

If you want to see this working solution, there's a link here to take you to the solution code.

So you can do that.

For part B, the brake lights should be on as your programme starts to indicate that the buggy is at a standstill.

So let's have a look at this code again together here.

So we have functions on the left hand side to turn the brake lights on and off.

We have on the right hand side setting up the LED pins.

So that's exactly the same as we've seen before.

So we have break underscore lights underscore on.

We are flashing the headlights or calling the flash underscore headlights function and getting it to do that three times.

We are sleeping for five seconds and then we are turning the brake lights off.

For part C, after the five seconds are up, your vehicle should proceed to move around the circuit.

It should turn off the brake lights before it moves.

So we are setting up the motor pins here.

So we have left underscore motor underscore forward which is equal to pin 14 pin.

out and then we have our left motor backwards, our right motor forward and our right motor backwards all assigned to different pins.

And then we're setting up the optical sensor pins here as well.

So left sensor is going to be equal to pin 11,.

pinin and the right sensor is going to be on pin 10.

We have a look at this solution code continued.

So we have our functions to control the buggy movement.

So we have a function for stop, which is going to print the text stopping.

It's then going to turn all of the motors too low, which is off and it's going to turn the brake lights on for forward.

We're going to print the text moving forward, we are going to turn the forward motors to high to turn those on and the backward motors off.

We're gonna turn our headlights on and we're going to turn our brake lights off.

To turn right, remember we need to have the motors moving in different directions.

So we have left underscore motor underscore forward to high and left underscore motor underscore backward to low, and then the rear motors are both set to low.

We're turning the headlights on and the brake lights off.

And then very similar for turning left, however the left motors are off and the right motor, which is in the forward direction is going to be high.

Then we have the final part of the code, which is going to call all of that.

So we have our while true loop and we have left underscore detect is equal to left underscore sensor.

And then we have the same for the right sensor this time calling it right underscore detect.

And then we have some selection statement.

So we have, if left underscore detect is equal to zero and right underscore detect is equal to zero, then we are going to move our buggy forward because it's on the right line or the right course.

We then have a EIF statement.

So if the left detect is equal to one and the right detect is equal to zero, we're going to turn right And then another EIF statement, if it's the opposite and it needs to turn left.

And then finally for our else, we have our stop.

For part D, as it travels around, your buggy should constantly check its distance to anything in front of it.

So we're going to use our ultrasonic sensor here.

So we have our trig is equal to pin 20 and our echo, which is going to be equal to 21.

And and then we have our function on the right hand side, which is def get underscore distance.

So trig.

low, utime.

sleep underscore us for ultrasonic is set to two.

Trig,high so turning the trig on and then we're using the sleep module again for 10 seconds and then we're turning the trig back off again.

And then we are calculating the time there, to calculate the distance.

So direction is equal to time underscore pulse underscore US (echo, 1, 30,000) and then distance centimetres is equal to duration multiplied by 0.

0343.

And we're dividing that value by two, and returning the distance centimetres.

And then while two is we are saying that distance is equal to get underscore distance.

So we're calling that function.

Remember if you want to see the working solution code, you can click the link in the slide deck to go and see that.

For part E, when an obstacle is placed in front of the buggy, it should come to a standstill to avoid hitting the obstacle.

And then it should apply the brake lights.

So we've got our function to control the movement, which is the stop function.

So we're printing, stopping, we are turning all of the motors off and we're turning the brake lights on.

And then on the right hand side we have our block of code.

So we have, while true distances equal to get underscore distance and then we have a selection statement.

So if distance is less than 20 with a colon, we are going to call the function stop.

We're going to print the message, obstacle detected, stopping, and then we have utime.

sleep 0.

1.

And then we have continue to to carry on with the programme.

Okay, we've come to the end of today's lesson creating an autonomous vehicle.

And hopefully you've got your buggies working, which will be fantastic.

Let's summarise what we've learned in this lesson.

And LED is a small electronic component that emits light when electricity passes through it.

LEDs are used in a wide variety of products from devices around the home to vehicle lighting and signals.

LEDs are output components and can be controlled with code like other components you may have already used.

Microcontrollers control multiple outputs sequentially.

That means one by one.

Modular code can make projects easier to understand by breaking down code into smaller, simpler parts.

I hope you've enjoyed today's lesson and I hope you'll join me soon.

Bye.

Files you will need for this lesson

Download these files to use in the lesson.
  • L6TaskBActivity3aSolutionV2931 B (PY)
  • L6TaskBActivity3eSolutionV22.93 KB (PY)
  • L6TaskBActivity3bSolutionV2931 B (PY)
  • L6TaskBActivity3dSolutionV22.82 KB (PY)
  • L6TaskBActivity3cSolutionV22.41 KB (PY)