A web developer playing with the Intel Galileo part 3: Analog Output (PWM)

So, now that we have figured out digital input/output and analog input it is time for the Analog Output… or rather, a simulation of it using the technique known as Pulse Width Modulation, or PWM. If you have some bases on electronics this term should not be anything new for you. If not, take a look at one of the very good tutorials online on how it all work. It is highly recommended you get a basic idea of how it works, since PWM is one of the most widely used techniques in electronic for controlling something like the speed of a motor or a led’s brightness. You probably use PWM on a daily basis without realizing it.

The Galileo is capable of outputting a PWM wave. Here is how we make it work from Javascript.

First, take another look at the digital pins.

galileohiresmin

If you look closely, you will realize there is sinusoidal sign next tome some pins (a squiggle). This marks which pins are capable of PMW output.

Similar to our previous cases, managing this pins is a matter of writing to a number of specific files, this time on the /sys/class/pwm/pwmchip0/ folder. The steps are simple:

  1. We write to the export file with the number of the PWM Channel we want to use
  2. Inside the folder corresponding to the channel we write 1 to the enable file
  3. We write a number (in nanoseconds) that corresponds to the period of the wave to the period file
  4. Finally, we write a length (in nanoseconds) that corresponds to the duty cycle of the wave to the duty_cycle file.

Once again the PWM channel number and the pin number are not always the same, so we must create a variable to map them out:

 


var PWMmapping = {
	"3": 3,
	"5": 5,
	"6": 6,
	"9": 1,
	"10": 7,
	"11": 4
}

With that done, let us make a pair of functions for steps 1 and 2. A function that exports the desired PWM channel and one that writes to it for enabling.


exportPWM = function(pwm_w, callback) {
	fs.writeFile('/sys/class/pwm/pwmchip0/export', pmw_w, fileOptions, function (err) {
		if (err) { 
			console.log("export error");
			console.log(err);
		}
		if(callback){
				callback();
			}
});
};

galileo.writePWM = function(pwm_wr, value, callback){
	if(value != 0 && value !=1){
		console.log("PWM write only supports binary values");
		return;
	}
	fs.writeFile("/sys/class/pwm/pwmchip0/pwm" + pwm_wr + "/enable", value, fileOptions, function(err, data) {
		if (err) { console.log("Writing " + pwm_wr + " " + value); }
		if (callback){ callback();}
	});
}

For easier future usage, we can put this into one simple prepare function:


galileo.preparePWM = function(pwm_w, callback) {
	exportPWM(pwm_w, function(){
		galileo.writePWM(pwm_w, 1, function(){
			callback();
		});
	});
}

With that done, all we need is functions that set up the period and duty cycle:


galileo.setPWMperiod = function(pwm_wr, value, callback){
	fs.writeFile("/sys/class/pwm/pwmchip0/pwm" + pwm_wr + "/period", value, fileOptions, function(err, data) {
		if (err) { console.log("Period " + pwm_wr + " " + value); }
		if (callback){ callback();}
	});
}

galileo.setPWMdutycycle = function(pwm_wr, value, callback){
	fs.writeFile("/sys/class/pwm/pwmchip0/pwm" + pwm_wr + "/duty_cycle", value, fileOptions, function(err, data) {
		if (err) { console.log("duty_cycle " + pwm_wr + " " + value); }
		if (callback){ callback();}
	});
}

Advertisements

An upcoming web developer and android app-maker with an interest on using reliable tech in creative ways, open source projects and start-ups.

Tagged with: , , ,
Posted in devices
3 comments on “A web developer playing with the Intel Galileo part 3: Analog Output (PWM)
  1. haridasgowra says:

    good post………..informative………

  2. Connor says:

    Hi,

    Would it be possible to post the full program? I can’t seem to get an example working.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: