Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying BeagleBone By Example
  • Table Of Contents Toc
  • Feedback & Rating feedback
BeagleBone By Example

BeagleBone By Example

By : Prabakar
5 (1)
close
close
BeagleBone By Example

BeagleBone By Example

5 (1)
By: Prabakar

Overview of this book

BeagleBone is a low cost, community-supported development platform to develop a variety of electronic projects. This book will introduce you to BeagleBone and get you building fun, cool, and innovative projects with it. Start with the specifications of BeagleBone Black and its operating systems, then get to grips with the GPIOs available in BeagleBone Black. Work through four types of exciting projects: building real-time physical computing systems, home automation, image processing for a security system, and building your own tele-controlled robot and learn the fundamentals of a variety of projects in a single book. By the end of this book, you will be able to write code for BeagleBone in order to operate hardware and impart decision-making capabilities with the help of efficient coding in Python.
Table of Contents (12 chapters)
close
close
11
Index

Writing your own Python program on BeagleBone board

In this section, we will write our first few Python codes on your BeagleBone board. That will take an input and make a decision based on the input and print out an output depending on the code that we write. There are three sample codes in this topic as examples, which will help you cover some fundamentals of any programming language, including defining variables, using arithmetic operators, taking input and printing output, loops and decision making algorithm.

Before we write and execute a python program, let us get into python's interactive shell interface via the Linux shell and try some basic things like creating a variable and performing math operations on those variables.

To open the python shell interface, you just have the type python on the Linux shell like you did for any Linux shell command in the previous section of this chapter.

Once you type python and hit Enter, you should be able to see the terminal as shown in the following screenshot:

Writing your own Python program on BeagleBone board

Now you are into python's interactive shell interface where every line that you type is the code that you are writing and executing simultaneously in every step. To learn more about this, visit https://www.python.org/shell/ or to get started and learn python programming language you can get our Python By Example book in our publication.

Let's execute a series of syntax in python's interactive shell interface to see whether it's working.

Let's create a variable A and assign value 20 to it:

Writing your own Python program on BeagleBone board

Now let's print A to check what value it is assigned:

Writing your own Python program on BeagleBone board

You can see that it prints out the value that we stored on it.

Now let's create another variable named B and store value 30 to it:

Writing your own Python program on BeagleBone board

Let's try adding these two variables and store the result in another variable named C. Then print C where you can see the result of A+B, that is, 50.

Writing your own Python program on BeagleBone board

That is the very basic calculation we performed on a programming interface. We created two variables with different values and then performed an arithmetic operation of adding two values on those variables and printed out the result.

Now, let's get a little more ahead and store string of characters in a variable and print them.

Writing your own Python program on BeagleBone board

Wasn't that simple. Like this you can play around with python's interactive shell interface to learn coding. But any programmer would like to write a code and execute the program to get the output on a single command right.

Let's see how that can be done now.

To get out of the Python's Interactive Shell and get back to the current working directory on Linux Shell, just hold the Ctrl button and press D, that is, Ctrl + D on the keyboard. You will be back on the Linux Shell interface as shown next:

Writing your own Python program on BeagleBone board

Now let's go ahead and write the program to perform the same action that we tried executing on python's interactive shell. That is to store two values on different variables and print out the result when both of them are added. Let's add some spice to it by doing multiple arithmetic operations on the variables that we create and print out the values of addition and subtraction.

You will need a text editor to write programs and save them. You can do it using the cat command also. But in future when you use indentation and more editing on the programs, the basic cat command usage will be difficult. So, let's start using the available text editor on Linux named nano, which is one of my favorite text editors in Linux. If you have a different choice and you are familiar with other text editors on Linux, such as vi or anything else, you can go ahead and continue the next steps using them to write programs.

To create a python file and start writing the code on it using nano, you need to use the nano command followed by the filename with extension .py.

Let's create a file named ArithmeticOperations.py.

Writing your own Python program on BeagleBone board

Once you type this command, the text editor will open up.

Writing your own Python program on BeagleBone board

Here you can type your code and save it using the keyboard command Ctrl + X.

Let's go ahead and write the code which is shown in the following screenshot and let's save the file using Ctrl + X:

Writing your own Python program on BeagleBone board

Then type Y when it prompts to save the modified file.

Writing your own Python program on BeagleBone board

Then if you want to change the file with a different name, you can change it in the next step before you save it. Since we created the file now only, we don't need to do it. In case if you want to save the modified copy of the file in future with a different name, you can change the filename in this step:

Writing your own Python program on BeagleBone board

For now we will just hit Enter that will take us back to the Linux Shell and the file AritmeticOperations.py will be created inside the current working directory, which you can see by typing the ls command. You can also see the contents of the file by typing the more command that we learned in the previous section of this chapter.

Writing your own Python program on BeagleBone board

Now let's execute the python script and see the output. To do this, you just have to type the command python followed by the python program file that we created, that is, the ArithmeticOperations.py.

Writing your own Python program on BeagleBone board

Once you run the python code that you wrote, you will see the output as shown earlier with the results as output.

Now that you have written and executed your first code on python and tested it working on your BeagleBone board, let's write another python code, which is shown in the following screenshot where the code will ask you to enter an input and whatever text you type as input will be printed on the next line and the program will run continuously.

Let's save this python code as InputPrinter.py:

Writing your own Python program on BeagleBone board

In this code, we will use a while loop so that the program runs continuously until you break it using the Ctrl + D command where it will break the program with an error and get back to Linux Shell.

Writing your own Python program on BeagleBone board

Now let's try out our third and last program of this section and chapter, where when we run the code, the program asks the user to type the user's name as input and if they type a specific name that we compare, it prints and says Hello message or if a different name was given as input, it prints Go Away; let's call this code Say_Hello_To_Boss.py.

Writing your own Python program on BeagleBone board

Instead of my name Jayakarthigeyan, you can replace it with your name or any string of characters on comparing which, the output decision varies.

When you execute the code, the output will look as shown in the following screenshot:

Writing your own Python program on BeagleBone board

Like we did in the previous program, you can hit Ctrl + D to stop the program and get back to Linux Shell.

In this way, you can work with python programming language to create codes that can run on the BeagleBone boards in the way you desire.

Since we have come to the end of this chapter, let's give a break to our BeagleBone board.

Let's power off our BeagleBone board using the command sudo poweroff, which will shut down the operating system.

Writing your own Python program on BeagleBone board

After you execute this command, if you get the error message shown in the following screenshot, it means the BeagleBoard has powered off.

Writing your own Python program on BeagleBone board

You can turn off the power supply that was connected to your BeagleBone board now.

Create a Note

Modal Close icon
You need to login to use this feature.
notes
bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Delete Note

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Edit Note

Modal Close icon
Write a note (max 255 characters)
Cancel
Update Note

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY