Pulse a LED With the BeagleBone and C++

by wgaona in Circuits > Microcontrollers

747 Views, 1 Favorites, 0 Comments

Pulse a LED With the BeagleBone and C++

Cover.png

In this instructable, I start applying the PWM technique to pulse a LED. This is inspired by the Sparkfun work titled: Pulse a LED, where the brightness of a Led is changed using a sine wave, instead of a classic ramp or linear function.

The most remarkable aspect is that it uses a pure C++ code to control de PWM pin.

Circuit and Components

Circuit_bb.jpg

The circuit can be seen in the next figure. Please keep in mind that the BeagleBone works at 3.3V and not 5V like microcontrollers as Arduino. It is so muy important to avoid damage to the board.

The components are:

  • 5 Resistor of 1KΩ
  • 1 LED of 3mm of any color. In this case, I am using a white one.
  • Jumpers male-male to make the connections

Coding

The whole C++ library to control the BeagleBone is hosted in GitHub here. You only have to download to your own BeagleBone Black and put it in your favorite directory, for example: /home/debian/ if you are using the last official distribution by Beagleboard.

Create a new folder called PulseALed inside of /home/debian/BeagleCPP/Chapter04_PWM/.

Inside this new directory, create a new file called pulseALed.cpp and include the next code:

#include <iostream>
#include <cmath>
#include "../../Sources/PWM.h"

using namespace std;

// Global pwm pin declaration
PWM pwmWhiteLedPin(P8_19,600000);

bool stopPulse = false;
int PulseLed()
{
  while (stopPulse == false)
  {
    for (float in = 0; in < 6.28; in += 0.0628)
    {
      float out = sin(in) * 50 + 50;
      pwmWhiteLedPin.SetDutyCycle(out);
      pwmWhiteLedPin.Delayms(10);
    }
  }
  return 0;
}

int main()
{
  string message = "Main program starting here...";
  cout << RainbowText(message,"Blue", "White", "Bold") << endl;
  
  message = "Pulse a white led";
  cout << RainbowText(message, "White") << endl;

  // Call the function to pulse the LED
  pwmWhiteLedPin.DoUserFunction(&PulseLed);
  
  char userInput = '\0';
  while (userInput != 'y')
  {
    message = "Do you want to stop the pulse on the led? Enter 'y' for yes: ";
    cout << RainbowText(message, "Violet");
    cin >> userInput;
    if (userInput == 'y') 
    {
      // Stop the function
      stopPulse = true;
      
      // Clean the pwm value on pin
      pwmWhiteLedPin.SetDutyCycle(0);
    }
  }

  message = "Main program finishes here...";
  cout << RainbowText(message,"Blue", "White","Bold") << endl;

  return 0;
}

A brew explanation of the code is given here:

1. First, a PWM class object is declared, initializing the pin and its period:

// Global pwm pin declaration
PWM pwmWhiteLedPin(P8_19,600000);

2. After, a user function is defined. This function changes the duty cycle using a sine wave on 100 samples in a range of values between 0 and 100. This function uses a flag to stop it when the user presses a specific key.

bool stopPulse = false;
int PulseLed()
{
  while (stopPulse == false)
  {
    for (float in = 0; in < 6.28; in += 0.0628)
    {
      float out = sin(in) * 50 + 50;
      pwmWhiteLedPin.SetDutyCycle(out);
      pwmWhiteLedPin.Delayms(10);
    }
  }
  return 0;
}

3. In the main code, this PulseLed() function is used and a function pointer argument to the PWM class method DoUserFunction(&) which inside the class constructs a thread to run this function in parallel with the main program.

// Call the function to pulse the LED
pwmWhiteLedPin.DoUserFunction(&PulseLed);

4. Finally, the main program is always waiting for the press of "y" key to finish the program and stops the PulseLed() function.

char userInput = '\0';
while (userInput != 'y')
{
  message = "Do you want to stop the pulse on the led? Enter 'y' for yes: ";
  cout << RainbowText(message, "Violet");
  cin >> userInput;
  if (userInput == 'y') 
  {
    // Stop the function
    stopPulse = true;
    // Clean the pwm value on pin
    pwmWhiteLedPin.SetDutyCycle(0);
  }
}

Compiling

In order to run the code, you can make a bash script to compile the necessary files for the application.

1. Inside the same directory PulseALed, create a new file called, for example: build with the next lines:

#!/bin/bash

echo "Building BeagleBone Application..."

g++ -std=c++17 ../../Sources/RAINBOWCOLORS.cpp  ../../Sources/SYSFILEACCESS.cpp ../../Sources/PWM.cpp pulseALed.cpp -lpthread -o pulseALed.out

echo "Finished"

2. Change the permissions of this build file to execution: (Remember the default password: temppwd)

sudo chmod 755 build

3. To compile, all you have to do is execute the next command from the terminal:

./build

Execute

Pulse a LED With the BeagleBone and C++

From the terminal, all that you have to do is:

sudo ./pulseALed.out

Note: Sometimes is necessary to run it twice, before the application works completely.

The video of the application pulsing a led can be found here: