Creating and running programs¶

Now that y'all've ready up your computer and EV3 Brick, yous're set to get-go writing programs.

To make information technology easier to create and manage your programs, permit's first have a quick expect at how MicroPython projects and programs for your EV3 robots are organized.

Programs are organized into project folders, equally shown in Figure eight. A project folder is a directory on your calculator that contains the main program (main.py) and other optional scripts or files. This projection folder and all of its contents will be copied to the EV3 Brick, where the main program volition be run.

This page shows yous how to create such a project and how to transfer it to the EV3 Brick.

projectstructure

Figure 8 A project contains a programme called main.py and optional resources like sounds or MicroPython modules.

Creating a new project¶

To create a new project, open the EV3 MicroPython tab and click create a new projection, as shown in Figure 9. Enter a project name in the text field that appears and printing Enter. When prompted, choose a location for this program and confirm by clicking choose folder.

newproject

Effigy 9 Creating a new project. This example is chosen getting_started, simply you can cull whatsoever proper name.

When y'all create a new project, it already includes a file chosen main.py. To see its contents and to modify information technology, open it from the file browser every bit shown in Figure 10. This is where you lot'll write your programs.

If you are new to MicroPython programming, nosotros recommend that you lot proceed the existing code in place and add together your code to it.

projectoverview

Effigy 10 Opening the default principal.py programme.

Opening an existing project¶

To open a projection you created previously, click File and click Open Binder, as shown in Figure 11. Next, navigate to your previously created project folder and click OK. Y'all can as well open your recently used projects using the Open up Recent menu option.

existingproject

Figure 11 Opening a previously created projection.

Connecting to the EV3 Brick with Visual Studio Lawmaking¶

To be able to transfer your code to the EV3 Brick, you'll showtime need to connect the EV3 Brick to your reckoner with the mini-USB cable and configure the connection with Visual Studio Code. To do so:

  • Plow the EV3 Brick on
  • Connect the EV3 Brick to your reckoner with the mini-USB cablevision
  • Configure the USB connection as shown in Figure 12.

connecting

Effigy 12 Configuring the USB connexion between the computer and the EV3 Brick

Downloading and running a plan¶

Yous can printing the F5 key to run the program. Alternatively, you tin start it manually by going to the debug tab and clicking the light-green start arrow, as shown in Effigy 13.

When the plan starts, a pop-up toolbar allows you to cease the program if necessary. Y'all can also stop the program at any fourth dimension using the back button on the EV3 Brick.

If your program produces whatsoever output with the print command, this is shown in the output window.

running

Figure thirteen Running a plan

Expanding the example programme¶

Now that you've run the basic code template, yous can expand the program to make a motor move. Kickoff, attach a Large Motor to Port B on the EV3 Brick, every bit shown in Figure xiv.

firstprogram

Figure xiv The EV3 Brick with a Big Motor attached to port B.

Next, edit main.py to make information technology look like this:

                            #!/usr/bin/env pybricks-micropython              from              pybricks.hubs              import              EV3Brick              from              pybricks.ev3devices              import              Motor              from              pybricks.parameters              import              Port              # Create your objects here              # Initialize the EV3 Brick.              ev3              =              EV3Brick              ()              # Initialize a motor at port B.              test_motor              =              Motor              (              Port              .              B              )              # Write your program here              # Play a sound.              ev3              .              speaker              .              beep              ()              # Run the motor upward to 500 degrees per 2nd. To a target bending of 90 degrees.              test_motor              .              run_target              (              500              ,              90              )              # Play some other beep sound.              ev3              .              speaker              .              beep              (              frequency              =              1000              ,              duration              =              500              )            

This program makes your robot beep, rotate the motor, and beep once more with a higher pitched tone. Run the program to make sure that it works equally expected.

Managing files on the EV3 Brick¶

After you've downloaded a project to the EV3 Brick, you lot can run, delete, or back up programs stored on it using the device browser equally shown in Figure 15.

files

Figure 15 Using the EV3 device browser to manage files on your EV3 Brick