35 min read

In this article by Jayakarthigeyan Prabakar, authors of BeagleBone By Example, we will discuss steps to get started with your BeagleBone board to build real-time physical computing systems using your BeagleBone board and Python programming language. This article will teach you how to set up your BeagleBone board for the first time and write your first few python codes on it.

(For more resources related to this topic, see here.)

By end of this article, you would have learned the basics of interfacing electronics to BeagleBone boards and coding it using Python which will allow you to build almost anything from a home automation system to a robot through examples given in this article.

Firstly, in this article, you will learn how to set up your BeagleBone board for the first time with a new operating system, followed by usage of some basic Linux Shell commands that will help you out while we work on the Shell Terminal to write and execute python codes and do much more like installing different libraries and software on your BeagleBone board. Once you get familiar with usage of the Linux terminal, you will write your first code on python that will run on your BeagleBone board. Most of the time, we will using the freely available open-source codes and libraries available on the Internet to write programs on top of it and using it to make the program work for our requirement instead of entirely writing a code from scratch to build our embedded systems using BeagleBone board. The contents of this article are divided into:

  • Prerequisites
  • About the single board computer – BeagleBone board
  • Know your BeagleBone board
  • Setting up your BeagleBone board
  • Working on Linux Shell
  • Coding on Python in BeagleBone Black

Prerequisites

This topic will cover what parts you need to get started with BeagleBone Black. You can buy them online or pick them up from any electronics store that is available in your locality.

The following is the list of materials needed to get started:

  • 1x BeagleBone Black
  • 1x miniUSB type B to type A cable
  • 1x microSD Card (4 GB or More)
  • 1x microSD Card Reader
  • 1x 5V DC, 2A Power Supply
  • 1x Ethernet Cable

There are different variants of BeagleBone boards like BeagleBone, BeagleBone Black, BeagleBone Green and some more old variants. This articlewill mostly have the BeagleBone Black shown in the pictures. Note that BeagleBone Black can replace any of the other BeagleBone boards such as the BeagleBone or BeagleBone Green for most of the projects. These boards have their own extra features so to say. For example, the BeagleBone Black has more RAM, it has almost double the size of RAM available in BeagleBone and an in-built eMMC to store operating system instead of booting it up only through operating system installed on microSD card in BeagleBone White. Keeping in mind that this articleshould be able to guide people with most of the BeagleBone board variants, the tutorials in this articlewill be based on operating system booted from microSD card inserted on the BeagleBone board. We will discuss about this in detail in the Setting up your BeagleBone board and installing operating system’s topics of this article.

BeagleBone Black – a single board computer

This topic will give you brief information about single board computers to make you understand what they are and where BeagleBone boards fit inside this category.

Have you ever wondered how your smartphones, smart TVs, and set-top boxes work?

All these devices run custom firmware developed for specific applications based on the different Linux and Unix kernels. When you hear the word Linux and if you are familiar with Linux, you will get in your mind that it’s nothing but an operating system, just like Windows or Mac OS X that runs on desktops and server computers. But in the recent years the Linux kernel is being used in most of the embedded systems including consumer electronics such as your Smartphones, smart TVs, set-top boxes, and much more. Most people know android and iOS as an operating system on their smart phones. But only a few know that, both these operating systems are based on Linux and Unix kernels.

Did you ever question how they would develop such devices? There should be a development board right? What are they?

This is where Linux Development boards like our BeagleBone boards are used.

By adding peripherals such as touch screens, GSM modules, microphones, and speakers to these single board computers and writing the software that is the operating system with graphical user interface to make them interact with the physical world, we have so many smart devices now that people use every day.

Nowadays you have proximity sensors, accelerometers, gyroscopes, cameras, IR blasters, and much more on your smartphones. These sensors and transmitters are connected to the CPU on your phone through the Input Output ports on the CPU, and there is a small piece of software that is running to communicate with these electronics when the whole operating system is running in the Smartphone to get the readings from these sensors in real-time. Like the autorotation of screen on the latest smartphones. There is a small piece of software that is reading the data from accelerometer and gyroscope sensors on the phone and based on the orientation of the phone it turns the graphical display.

So, all these Linux Development boards are tools and base boards using which you can build awesome real world smart devices or we can call them physical computing systems as they interact with the physical world and respond with an output.

Getting to know your board – BeagleBone Black

BeagleBone Black can be described as low cost single board computer that can be plugged into a computer monitor or TV via a HDMI cable for output and uses standard keyboard and mouse for input. It’s capable of doing everything you’d expect a desktop computer to do, like playing videos, word processing, browsing the Internet, and so on. You can even setup a web server on your BeagleBone Black just like you would do if you want to set up a webserver on a Linux or Windows PC.

But, differing from your desktop computer, the BeagleBone boards has the ability to interact with the physical world using the GPIO pins available on the board, and has been used in various physical computing applications. Starting from Internet of Things, Home Automation projects, to Robotics, or tweeting shark intrusion systems. The BeagleBone boards are being used by hardware makers around the world to build awesome physical computing systems which are turning into commercial products also in the market. OpenROV, an underwater robot being one good example of what someone can build using a BeagleBone Black that can turn into a successful commercial product.

Hardware specification of BeagleBone Black

A picture is worth a thousand words. The following picture describes about the hardware specifications of the BeagleBone Black. But you will get some more details about every part of the board as you read the content in the following picture.

If you are familiar with the basic setup of a computer. You will know that it has a CPU with RAM and Hard Disk. To the CPU you can connect your Keyboard, Mouse, and Monitor which are powered up using a power system.

The same setup is here in BeagleBone Black also. There is a 1GHz Processor with 512MB of DDR3 RAM and 4GB on board eMMC storage, which replaces the Hard Disk to store the operating system. Just in case you want more storage to boot up using a different operating system, you can use an external microSD card that can have the operating system that you can insert into the microSD card slot for extra storage.

As in every computer, the board consists of a power button to turn on and turn off the board and a reset button to reset the board. In addition, there is a boot button which is used to boot the board when the operating system is loaded on the microSD card instead of the eMMC. We will be learning about usage of this button in detail in the installing operating systems topic of this article.

There is a type A USB Host port to which you can connect peripherals such as USB Keyboard, USB Mouse, USB Camera, and much more, provided that the Linux drivers are available for the peripherals you connect to the BeagleBone Black.

It is to be noted that the BeagleBone Black has only one USB Host Port, so you need to get an USB Hub to get multiple USB ports for connecting more number of USB devices at a time. I would recommend using a wireless Keyboard and Mouse to eliminate an extra USB Hub when you connect your BeagleBone Black to monitor using the HDMI port available.

The microHDMI port available on the BeagleBone Black gives the board the ability to give output to HDMI monitors and HDMI TVs just like any computer will give.

You can power up the BeagleBone Black using the DC Barrel jack available on the left hand side corner of the board using a 5V DC, 2A adapter. There is an option to power the board using USB, although it is not recommended due to the current limit on USB ports.

There are 4 LEDs on board to indicate the status of the board and help us for identifications to boot up the BeagleBone Black from microSD card. The LEDs are linked with the GPIO pins on the BeagleBone Black which can be used whenever needed.

You can connect the BeagleBone Black to the LAN or Internet using the Ethernet port available on the board using an Ethernet cable. You can even use a USB Wi-Fi module to give Internet access to your BeagleBone Black.

The expansion headers which are in general called the General Purpose Input Output (GPIO) pins include 65 digital pins. These pins can be used as digital input or output pins to which you can connect switches, LEDs and many more digital input output components, 7 analog inputs to which you can connect analog sensors like a potentiometer or an analog temperature sensor, 4 Serial Ports to which you can connect Serial Bluetooth or Xbee Modules for wireless communication or anything else, 2 SPI and 2 I2C Ports to connect different modules such as sensors or any other modules using SPI or I2C communication.

We also have the serial debugging port to view the low-level firmware pre-boot and post-shutdown/reboot messages via a serial monitor using an external serial to USB converter while the system is loading up and running. After booting up the operating system, this also acts as a fully interactive Linux console.

Setting up your BeagleBone board

Your first step to get started with BeagleBone Boards with your hands on will be to set it up and test it as suggested by the BeagleBone Community with the Debian distribution of Linux running on BeagleBone Black that comes preloaded on the eMMC on board. This section will walk you through that process followed by installing different operating system on your BeagleBone board and log in into it. And then get into start working with files and executing Linux Shell commands via SSH.

  1. Connect your BeagleBone Black using the USB cable to your Laptop or PC.

    This is the simplest method to get your BeagleBone Black up and running. Once you connect your BeagleBone Black, it will start to boot using the operating system on the eMMC storage. To log in into the operating system and start working on it, the BeagleBone Black has to connect to a network and the drivers that are provided by the BeagleBoard manufacturers allow us to create a local network between your BeagleBone Black and your computer when you connect it via the USB cable. For this, you need to download and install the device drivers provided by BeagleBone board makers on your PC as explained in step 2.

  2. Download and install device drivers.

    • Once the installation is complete, click on Finish. It is shown in the following screenshot:

    • Once the installation is done, restart your PC. Make sure that the Wi-Fi on your laptop is off and also there is no Ethernet connected to your Laptop. Because now the BeagleBone Black device drivers will try to create a LAN connection between you laptop and BeagleBone Black so that you can access the webserver running by default on the BeagleBone Black to test it’s all good, up, and running. Once you reboot your PC, get to step 3.

  3. Connect to the Web Server Running on BeagleBone Black.

    Open your favorite web browser and enter the IP address 192.168.7.2 on the URL bar, which is the default static IP assigned to BeagleBone Black.This should open up the webpage as shown in the following screenshot:

    If you get a green tick mark with the message your board is connected. You can make sure that you got the previous steps correct and you have successfully connected to your board.

    If you don’t get this message, try removing the USB cable connected to the BeagleBone Black, reconnect it and check again. If you still don’t get it. Then check whether you did the first two steps correctly.

  4. Play with on board LEDs via the web server.

    If you Scroll down on the web page to which we got connected, you will find the section as shown in the following screenshot:

This is a sample setup made by BeagleBone makers as the first time interaction interface to make you understand what is possible using BeagleBone Black. In this section of the webpage, you can run a small script. When you click on Run, the On board status LEDs that are flashing depending on the status of the operating system will stop its function and start working based on the script that you see on the page. The code is running based on a JavaScript library built by BeagleBone makers called the BoneScript. We will not look into this in detail as we will be concentrating more on writing our own programs using python to work with GPIOs on the board. But to make you understand, here is a simple explanation on what is there on the script and what happens when you click on the run button on the webpage.

The pinMode function defines the on board LED pins as outputs and the digitalWrite function sets the state of the output either as HIGH or LOW. And the setTimeout function will restore the LEDs back to its normal function after the set timeout, that is, the program will stop running after the time that was set in the setTimeout function.

Say I modify the code to what is shown in the following screenshot:

You can notice that, I have changed the states of two LEDs to LOW and other two are HIGH and the timeout is set to 10,000 milliseconds.

So when you click on the Run Button. The LEDs will switch to these states and stay like that for 10 seconds and then restore back to its normal status indication routine, that is, blinking.

You can play around with different combinations of HIGH and LOW states and setTimeout values so that you can see and understand what is happening.

You can see the LED output state of BeagleBone Black in the following screenshot for the program we executed earlier:

You can see that the two LEDs in the middle are in LOW state. It stays like this for 10 seconds when you run the script and then it will restore back to its usual routine. You can try with different timeout values and states of LEDs on the script given in the webpage and try clicking on the run button to see how it works.

Like this we will be writing our own python programs and setting up servers to use the GPIOs available on the BeagleBone Black to make them work the way we desire to build different applications in each project that is available in this article.

Installing operating systems

We can make the BeagleBone Black boot up and run using different operating systems just like any computer can do. Mostly Linux is used on these boards which is free and open source, but it is to be noted that specific distributions of Linux, Android, and Windows CE have been made available for these boards as well which you can try out.

The stable versions of these operating systems are made available at http://beagleboard.org/latest-images.

By default, the BeagleBone Black comes preloaded with a Debian distribution of Linux on the eMMC of the board. However, if you want, you can flash this eMMC just like you do to your Hard Drive on your computer and install different operating systems on it.

As mentioned in the note at the beginning of this article, considering all the tutorials in this articleshould be useful to people who own BeagleBone as well as the BeagleBone Black. We will choose the recommended Debian package by www.BeagleBoard.org Foundation and we will boot the board every time using the operating system on microSD card.

Perform the following steps to prepare the microSD card and boot BeagleBone using that:

  1. Goto: http://beagleboard.org/latest-images.
  2. Download the latest Debian Image.

    The following screenshot highlights the latest Debian Image available for flashing on microSD card:

  3. Extract the image file inside the RAR file that was downloaded:

    You might have to install WinRAR or any .rar file extracting software if it is not available in your computer already.

  4. Install Win32 Disk Imager software.

    To write the image file to a microSD card, we need this software. You can go to Google or any other search engine and type win32 disk imager as keyword and search to get the web link to download this software as shown in the following screenshot:

    The web link, where you can find this software is http://sourceforge.net/projects/win32diskimager/. But this keeps changing often that’s why I suggest you can search it via any search engine with the keyword.

  5. Once you download the software and install it. You should be able to see the window as shown in the following screenshot when you open the Win32 Disk Imager:

    Now that you are all set with the software, using which you can flash the operating system image that we downloaded. Let’s move to the next step where you can use Win32 Disk Imager software to flash the microSD card.

  6. Flashing the microSD card.

    Insert the microSD into a microSD card reader and plug it onto your computer. It might take some time for the card reader to show up your microSD card. Once it shows up, you should be able to select the USB drive as shown in the following screenshot on the Win32 Disk Imager software.

  7. Now, click on the icon highlighted in the following screenshot to open the file explorer and select the image file that we extracted in Step 3:

  8. Go to the folder where you extracted the latest Debian image file and select it.

  9. Now you can write the image file to microSD card by clicking on the Write button on the Win32 Disk Imager. If you get a prompt as shown in the following screenshot, click on Yes and continue:

  10. Once you click on Yes, the flashing process will start and the image file will be written on to the microSD card. The following screenshot shows the flashing process progressing:

    Once the flashing is completed, you will get a message as shown in the following screenshot:

  11. Now you can click onOKexit the Win32 Disk Imager software and safely remove the microSD card from your computer.

Now you have successfully prepared your microSD card with the latest Debian operating system available for BeagleBone Black. This process is same for all other operating systems that are available for BeagleBone boards. You can try out different operating systems such as the Angstrom Linux, Android, or Windows CE others, once you get familiar with your BeagleBone board by end of this article.

For Mac users, you can refer to either https://learn.adafruit.com/ssh-to-beaglebone-black-over-usb/installing-drivers-mac or https://learn.adafruit.com/beaglebone-black-installing-operating-systems/mac-os-x.

Booting your BeagleBone board from a SD card

Since you have the operating system on your microSD card now, let us go ahead and boot your BeagleBone board from that microSD card and see how to login and access the filesystem via Linux Shell.

You will need your computer connected to your Router either via Ethernet or Wi-Fi and an Ethernet cable which you should connect between your Router and the BeagleBone board. The last but most important thing is an External Power Supply using which you will power up your BeagleBone board because power supply via a USB will be not be enough to run the BeagleBone board when it is booted from a microSD card.

  1. Insert the microSD card into BeagleBone board.

    Now you should insert the microSD card that you have prepared into the microSD card slot available on your BeagleBone board.

  2. Connect your BeagleBone to your LAN.

    Now connect your BeagleBone board to your Internet router using an Ethernet cable.

    You need to make sure that your BeagleBone board and your computer are connected to the same router to follow the next steps.

  3. Connect external power supply to your BeagleBone board.

  4. Boot your BeagleBone board from microSD card.

    On BeagleBone Black and BeagleBone Green, you have a Boot Button which you need to hold on while turning on your BeagleBone board so that it starts booting from the microSD card instead of the default mode where it starts to boot from the onboard eMMC storage which holds the operating system. In case of BeagleBone White, you don’t have this button, it starts to boot from the microSD card itself as it doesn’t have onboard eMMC storage.

Depending on the board that you have, you can decide whether to boot the board from microSD card or eMMC. Consider you have a BeagleBone Black just like the one I have shown in the preceding picture. You hold down the User Boot button that is highlighted on the image and turn on the power supply. Once you turn on the board while holding the button down, the four on-board LEDs will light up and stay HIGH as shown in the following picture for 1 or 2 seconds, then they will start to blink randomly.

Once they start blinking, you can leave the button.

Now your BeagleBone board must have started Booting from the microSD card, so our next step will be to log in to the system and start working on it. The next topic will walk you through the steps on how to do this.

Logging into the board via SSH over Ethernet

If you are familiar with Linux operations, then you might have guessed what this section is about. But for those people who are not daily Linux users or have never heard the term SSH, Secure Shell (SSH) is a network protocol that allows network services and remote login to be able to operate over an unsecured network in a secure manner. In basic terms, it’s a protocol through which you can log in to a computer and assess its filesystem and also work on that system using specific commands to create and work with files on the system.

In the steps ahead, you will work with some Linux commands that will make you understand this method of logging into a system and working on it.

  1. Setup SSH Software.

    To get started, log in to your BeagleBone board now, from a Windows PC, you need to install any SSH terminal software for windows.

    My favorite is PuTTY, so I will be using that in the steps ahead. If you are new to using SSH, I would suggest you also get PuTTY.

    The software interface of PuTTY will be as shown in the following screenshot:

    You need to know the IP address or the Host Name of your BeagleBone Black to log in to it via SSH. The default Host Name is beaglebone but in some routers, depending on their security settings, this method of login doesn’t work with Host Name. So, I would suggest you try to login entering the hostname first. If you are not able to login, follow Step 2. If you successfully connect and get the login prompt with Host Name, you can skip Step 2 and go to Step 3.

    But if you get an error as shown in the following screenshot, perform Step 2.

  2. Find an IP address assigned to BeagleBone board.

    Whenever you connect a device to your Router, say your computer, printer, or mobile phone. The router assigns a unique IP to these devices. The same way, the router must have assigned an IP to your BeagleBone board also. We can get this detail on the router’s configuration page of your router from any browser of a computer that is connected to that router.

    In most cases, the router can be assessed by entering the IP 192.168.1.1 but some router manufacturers have a different IP in very rare cases. If you are not able to assess your router using this IP 192.168.1.1, refer your router manual for getting access to this page.

    The images that are shown in this section are to give you an idea about how to log in to your router and get the IP address details assigned to your BeagleBone board from your router. The configuration pages and how the devices are shown on the router will look different depending on the router that you own.

    Enter the 192.168.1.1 address in you browser.

    When it asks for User Name and Password, enter admin as UserName and password as Password

    These are the mostly used credentials by default in most of the routers. Just in case you fail in this step, check your router’s user manual.

    Considering you logged into your router configuration page successfully, you will see the screen with details as shown in the following screenshot:

    If you click on the Highlighted part, Attached Devices, you will be able to see the list of devices with their IP as shown in the following screenshot, where you can find the details of the IP address that is assigned to your BeagleBone board.

    So now you can note down the IP that has been assigned to your BeagleBone board. It can be seen that it’s 192.168.1.14 in the preceding screenshot for my beaglebone board. We will be using this IP address to connect to the BeagleBone board via SSH in the next step.

  3. Connect via SSH using IP Address.

    Once you click on Open you might get a security prompt as shown in the following screenshot. Click on Yes and continue.

    Now you will get the login prompt on the terminal screen as shown in the following screenshot:

    If you got this far successfully, then it is time to log in to your BeagleBone board and start working on it via Linux Shell.

  4. Log in to BeagleBone board.

    When you get the login prompt as shown in the preceding screenshot, you need to enter the default username which is debian and default password which is temppwd. Now you should have logged into Linux Shell of your BeagleBone board as user with username debian.

Now that you have successfully logged into your BeagleBone board’s Linux Shell, you can start working on it using Linux Shell commands like anyone does on any computer that is running Linux.

The next section will walk you through some basic Linux Shell commands that will come in handy for you to work with any Linux system.

Working on Linux shell

Simply put, the shell is a program that takes commands from the keyboard and gives them to the operating system to perform. Since we will be working on BeagleBone board as a development board to build electronics projects, plugging it to a Monitor, Keyboard, and Mouse every time to work on it like a computer might be unwanted most of the times and you might need more resources also which is unnecessary all the time. So we will be using the shell command-line interface to work on the BeagleBone boards. If you want to learn more about the Linux command-line interfaces, I would suggest you visit to http://linuxcommand.org/.

Now let’s go ahead and try some basic shell commands and do something on your BeagleBone board.

You can see the kernel version using the command uname -r. Just type the command and hit enter on your keyboard, the command will get executed and you will see the output as shown here:

Next, let us check the date on your BeagleBone board:

Like this shell will execute your commands and you can work on your BeagleBone boards via the shell.

Getting kernel version and date was just for a sample test. Now let’s move ahead and start working with the filesystem.

  • ls: This stands for list command. This command will list out and display the names of folders and files available in the current working directory on which you are working.

  • pwd: This stands for print working directory command. This command prints the current working directory in which you are present.

  • mkdir: This stands for make directory command. This command will create a directory in other words a folder, but you need to mention the name of the directory you want to create in the command followed by it.

    Say I want to create a folder with the name WorkSpace, I should enter the command as follows:

    When you execute this command, it will create a folder named WorkSpace inside the current working directory you are in, to check whether the directory was created. You can try the ls command again and see that the directory named WorkSpace has been created.

To change the working directory and go inside the WorkSpace directory, you can use the next command that we will be seeing.

  • cd: This stands for change directory command. This command will help you switch between directories depending on the path you provide along with this command.

Now to switch and get inside the WorkSpace directory that you created, you can type the command as follows:

cd WorkSpace

You can note that whenever you type a command, it executes in the current working that you are in. So the execution of cd WorkSpace now will be equivalent to cd /home/debian/WorkSpace as your current working directory is /home/debian.

Now you can see that you have got inside the WorkSpace folder, which is empty right now, so if you type the ls command now, it will just go to the next line on the shell terminal, it will not output anything as the folder is empty.

Now if you execute the pwd command, you will see that your current working directory has changed.

  • cat: This stands for the cat command. This is one of the most basic commands that is used to read, write, and append data to files in shell.

To create a text file and add some content to it, you just need to type the cat command cat > filename.txt

Say I want to create a sample.txt file, I would type the command as shown next:

Once you type, the cursor will be waiting for the text you want to type inside the text file you created. Now you can type whatever text you want to type and when you are done press Ctrl + D. It will save the file and get back to the command-line interface.

Say I typed This is a test and then pressed Ctrl + D. The shell will look as shown next.

Now if you type ls command, you can see the text file inside the WorkSpace directory.

If you want to read the contents of the sample.txt file, again you can use the cat command as follows:

Alternatively, you can even use the more command which we will be using mostly:

Now that we saw how we can create a file, let’s see how to delete what we created.

  • rm: This stands for remove command. This will let you delete any file by typing the filename or filename along with path followed by the command.

Say now we want to delete the sample.txt file we created, the command can be either

rm sample.txt which will be equivalent to rm /home/debian/WorkSpace/sample.txt as your current working directory is /home/debian/Workspace.

After you execute this command, if you try to list the contents of the directory, you will notice that the file has been deleted and now the directory is empty.

Like this, you can make use of the shell commands work on your BeagleBone board via SSH over Ethernet or Wi-Fi.

Now that you have got a clear idea and hands-on experience on using the Linux Shell, let’s go ahead and start working with python and write a sample program on a text editor on Linux and test it in the next and last topic of this article.

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 article.

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

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 articlein 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:

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

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:

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.

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.

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:

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.

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

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:

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

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:

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 article.

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.

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:

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.

Now let’s try out our third and last program of this section and article, 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.

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:

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 article, 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.

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

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

Summary

So here we are at the end of this article. In this article, you have learned how to boot your BeagleBone board from a different operating system on microSD card and then log in to it and start coding in Python to run a routine and make decisions

On an extra note, you can take this article to another level by trying out a little more by connecting your BeagleBone board to an HDMI monitor using a microHDMI cable and connecting a USB Keyboard and Mouse to the USB Host of the BeagleBone board and power the monitor and BeagleBone board using external power supply and boot it from microSD Card and you should be able to see some GUI and you will be able to use the BeagleBone board like a normal Linux computer. You will be able access the files, manage them, and use Shell Terminal on the GUI also. If you own BeagleBone Black or BeagleBone Green, you can try out to flash the onboard eMMC using the latest Debian operating system and try out the same thing that we did using the operating system booted from microSD card.

Resources for Article:


Further resources on this subject:


1 COMMENT

  1. When you connect the BBB to your pc you see a start.html file. By clicking that you get a web page from where get an IP address i.e of the board itself, say 192.168.1.1. Now open your terminal in Ubuntu. Use the command ssh 192.168.1.1 (the ip address you got from the web page). This gets you into the Beaglebone Board. By the way you won’t get an IDE sort of window while connected to your PC in this way. You will need more idea of the linux commands to navigate further through the beaglebone.

LEAVE A REPLY

Please enter your comment!
Please enter your name here