MBED Full tutorial

 

MBED Tutorial 1 – Getting Started

 

Mbed is a free online development platform for ARM-based microcontrollers.

 

Microcontroller is a small controller that can run a few instructions, turning on/off things, etc.

 

They are extremely useful in decision making for circuits.

 

3 Microcontrollers are the Arduino, Rasberry PI, Tiva C Launchboard, and BeagleBone Black. This tutorial is how to program ARM Microcontrollers using MBED.




ARM stands for Acorn Risk Machines, because the British company, named Acorn, named it. ARM holdings now owns the IP and licensing technology, and many companies like NXP and Texas Instruments have their own ARM-Line Microcontrollers. They can handle 32 bits at a time.

There are many Cortex Series, including the A, R, and M series. (ARM haha). These are in a biomedical engineer’s alley. Each number indicates the respective computational power. Computational power relates inversely to the energy efficiency.



MBED is an online development platform that can program many M-Series microcontrollers, providing an elegant and efficient way to test these microcontrollers.

 

You can write whatever code you want in the file named main.cpp. On the left, you can select which controller that you want to program with.



The library that is imported the most is the mbed.h library, which contains anything that really needs to run the MBED OS studio code. Compiling will compile the code to a binary file, 



The handbook list the APIs. 

developer.mbed.org  redirects stuff back to the API documentation.

developer.mbed.org/cookbook/homepage shows user exactly what to do. It has information on Slack, motors and actuators, etc. One of the key perks about the MBED webpage is that people actually update their code and stuff. Often, you also want to firmware update your device in MBED as well.

The following code is the “Hello World” code:

//This line contains all of the MBED libraries. 

#include "mbed.h"

 

//This line includes the code for the mouse library. 

#include "USBHostMouse.h"

#include <cstdint>

 

//This methods prints the position of the mouse. 

void onMouseEvent(uint8_t buttonsint8_t xint8_t yint8_t z){

    printf("buttons: %d, x: %d, y: %d, z: %d \f\n", buttons, x, y, z);

}

 

void mouse_task(void const *){

 

    //This line initializes the mouse.

    USBHostMouse mouse;

 

    //This line asynchronously polls the mouse signal until the mouse signal arrives. 

    while(!mouse.connect()){

        Thread::wait(500);

    }

 

    //attach the mouse even where 

    mouse.attachEvent(onMouseEvent);

 

    //This thread makes it that after the mouse is disconnected, the thread checks to see if 

    while(mouse.connected()){

        Thread::wait(500);

    }

 

    

}

 

//This thread executes upon running the code. 

int main(){

    //start the mouse thread, and print out the mouse attributes. 

    Thread.mouse_task(mouse_task, NULL, osPriorityNormal, 256 * 4);

    while(1){

        //This line blinks the LED every half-second. 

        led = !led;

        Thread::wait(500);

    }

}

 

 

Remember, you need internet for sure in order to run Mbed OS studio. This is because you need to install all of these libraries required to run the software.

 

MBED Tutorial 2 – Using Digital Pins as an Output

I will now be using the expensive Texas instruments BeagleBone Black for this. One of the most fundamental operations of a Microcontroller is to turn one of its pins on and off



The microcontroller has many, many, many pins, and the first thing to do is to locate the ground pin and connect it accordingly. The shorter metal pin is the cathode (positive) and the longer metal pin is the anode. Connect the cathode to the ground and the PDD pin to the anode source. A resistor is then used to connect the power line to the LED. In the BeagleBone Back Pins 1-2 is the ground, Pins 3-4 is the 3 volt, and pins 5-6 is the 5 volt.



MBED also has an online compiler interface.

The class DigitalOut allows the user to control the pins in the MBED OS studio, such that these pins can be driven high or low. This helps the BeagleBone Black interface with other digital things.

We need to tell what board in MBEd that should be run.

#include "mbed.h"

 

DigitalOut ledPinPTD_2 );

 

// main() runs in its own thread in the OS

int main()

{

    //This line of code turns the LED pin in the breadboard circuit one. 

    //The problem is it's gonna be really really fast. 

    ledPin = 0;   

    ledPin = 1;

 

}

 

   

 

The main issue with this code is that it’s going to be really, REALLY fast and there for you can’t properly turns on and off. The wait() property helps to save this.  This taks as its arguments the number of seconds where one wants to wait. The final program is as follows:

 

 

 

#include "mbed.h"

 

DigitalOut ledPinPTD_2 );

 

// main() runs in its own thread in the OS

int main()

{

    ledPin = 1;   

    //This line of code turns the LED pin in the breadboard circuit one.  

    //The problem is it's gonna be really really fast. 

    while(true){

        wait(0.5f);

        ledPin = 0;

        wait(0.5f);   

        ledPin = 1;

    }

   

 

}

 

   

 

Use mbed.org/compiler. After that, we hit the compile button, and it sends the binary file (01010100010…) to compile on the local machine (.bin). The BeagleBone us a USB folder, and all you have to do  is to move the file to the binary program, then hit the “reset” button. The digital pin would call the high state and the low state (3.3 Volts and Ground Pins, respectively.). If you flick a digital pin long enough, on average the result would be some nil value.

Your eyes cannot see anything above 60 Hertz. This also works well for motors and the like.



MBED Tutorial 3 - PWM

 

Really, you can quickly write code, and turn on and off. Pulse width modulation, or PDM for short, using a period pattern based on the pulse period. The duty cycle is the percentage of time in which the LED is turned on. By manipulating the duty cycle, you can essentially make the led move faster or slower, respectively.



The PwmOut class is provided the syntax, and use pwm to render the output, instead of setting the pin to be high or low.

We start off by setting the period of the PWM using a number function of the object called ledPWM.period(numseconds). If we cycle things fast enough, the eye then will have enough time to see the intermediate value.

We set the LED to the value and pause in order for things to not be ridiculously fast.

#include "mbed.h"

 

PwmOut ledPWM(PTD2);

 

int main() {

    

    //Run the period every one-onehundreth of a second. 

    ledPWM.period(0.01f);

 

    //set the LED to initially be off. 

    ledPWM = 0.0f;

 

    while(true)

    {

 

        //Increase the value of the LED

        for(float val = 0.0f; val < 1.0f; val += 0.05f){

            ledPWM = val;

            wait(0.05f);

        }

 

        //Decrease the value of the LED, as a result, making it "Breathe".

        for(float val = 1.0f; val > 0.0f; val -= 0.05f){

            ledPWM = val;

            wait(0.05f);

        }

 

    }

 

}

 

Dragging the binary to the MBED disk will automatically upload it into the device.

 

MBED Tutorial 4 – Using Digital Pins as an Input

We want digital pins to detect whether the voltage is 0V(low) or 3.3V(high). If the switch is connected to an input, we can use this in order to detect voltage. The input needs to pull to some voltage so when the switch Is open, one of the attributes will pull back to that voltage.

When the switch is open, the digital pin reads in a 0, and when it is closed, it reads in a 1.
The microcontrollers will open a high voltage by default. When a switch is closed, the microcontroller will read a low voltage, and turn on/off LED, depending on what it reads. This is a very simple input, depending on what the microcontroller reads in. 

The code for this is actually reasonably straightforward.

Every single time in the loop, I purposely check if the switch in the loop is on. If this is true, I set the LED to be 1. Else, I set the LEDPin to equal 0. I need to tell this digital pin to be a PullUp resistor. 

Now, the switch is going to connect to ground. When the switch is open, the input pin would be the high voltage, and therefore the LED will turn on. The following is the code to operate the pin: 


#include "mbed.h"

DigitalIn switchOn(PTD3PullUp);
DigitalOut ledPin(PTD2);

// main() runs in its own thread in the OS
int main() {

  while (true) {
    if (switchOn == true) {
      ledPin = 1;
    } else {
      ledPin = 0;
    }

  }
}

Beaglebone includes the IP address 192.168.7.2. 



The following diagram shows how to properly connect a board:  


Weird part is that the board shows 2.23V. 

Comments

Popular Posts