Trevor
0
- Joined
- Jul 17, 2009
- Messages
- 4,386
- Points
- 113
LPM Design: A Beginner's Guide
When I released OpenLPM in late 2011, the goal was to get more people tinkering with Arduino. But... there was one problem with OpenLPM. It had a bit of a steep learning curve. It sort of failed in its original objective to get a lot more people playing around with LPM's.
So here we are - I've decided to write a tutorial! Everything in this tutorial will be based on open source tools; primarily Arduino and Peregrine.
In this guide, I hope to prove that an LPM is just a conglomeration of very simple parts, and that it's totally possible to build one!
Part Zero - Getting Started
If this is your first time working with Arduino, I'd recommend going through the "Getting Started" guide, found here: Arduino - Getting Started
The first thing we'll do is upload the most basic program (also known as "sketches" by Arduino users) to the board so that we know it's working properly. This sketch is known as "Blink."
It simply blinks the onboard LED on and off.
A few things should be noted that are not covered by the comments in the code.
Rather than "naming" the pin, the statement "int led = 13;" is declaring an integer with the value being equal to the pin number; this just gives you the shorthand "led" to use instead of needing to remember what pin number the LED is hooked up to.
The statement "pinMode(led, OUTPUT);" sets that pin to an output mode. This allows it to be set at 0V or 5V (the two levels of the Arduino - logical LOW and logical HIGH), and allows you to control the LED using your code.
The statements enclosed by "/*" and "*/" are comments in the code and are ignored by the compiler. That encloses an entire block of lines as a comment. The other type of comment you'll see is preceded by "//" - it instructs the compiler to ignore anything on that line after the two slashes.
Lastly, the units for delay() are milliseconds.
With the super basics out of the way, let's move on to the basics!
Part One - Serial
At the core of any good LPM is datalogging, usually done over a serial line. This is prettymuch the easiest thing ever to do, so we'll start here.
Before we try to start sending any sort of actual data over the line, we'll start by just sending a dummy value.
You'll want to use the "Simple" protocol in Peregrine.
The graphing window will look like this:
Now, let's move to a changing value. We'll use a sine wave.
Our code now looks like this:
Your output now looks something like this:
So there you have it - the most very basic aspects of getting an Arduino to send data to your computer.
In the next part, we'll discuss actually collecting data and transmitting it.
I'll try to get that written tonight. Until then, have fun tinkering.
Trevor
When I released OpenLPM in late 2011, the goal was to get more people tinkering with Arduino. But... there was one problem with OpenLPM. It had a bit of a steep learning curve. It sort of failed in its original objective to get a lot more people playing around with LPM's.
So here we are - I've decided to write a tutorial! Everything in this tutorial will be based on open source tools; primarily Arduino and Peregrine.
In this guide, I hope to prove that an LPM is just a conglomeration of very simple parts, and that it's totally possible to build one!
Part Zero - Getting Started
If this is your first time working with Arduino, I'd recommend going through the "Getting Started" guide, found here: Arduino - Getting Started
The first thing we'll do is upload the most basic program (also known as "sketches" by Arduino users) to the board so that we know it's working properly. This sketch is known as "Blink."
It simply blinks the onboard LED on and off.
Code:
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop()
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
A few things should be noted that are not covered by the comments in the code.
Rather than "naming" the pin, the statement "int led = 13;" is declaring an integer with the value being equal to the pin number; this just gives you the shorthand "led" to use instead of needing to remember what pin number the LED is hooked up to.
The statement "pinMode(led, OUTPUT);" sets that pin to an output mode. This allows it to be set at 0V or 5V (the two levels of the Arduino - logical LOW and logical HIGH), and allows you to control the LED using your code.
The statements enclosed by "/*" and "*/" are comments in the code and are ignored by the compiler. That encloses an entire block of lines as a comment. The other type of comment you'll see is preceded by "//" - it instructs the compiler to ignore anything on that line after the two slashes.
Lastly, the units for delay() are milliseconds.
With the super basics out of the way, let's move on to the basics!
Part One - Serial
At the core of any good LPM is datalogging, usually done over a serial line. This is prettymuch the easiest thing ever to do, so we'll start here.
Before we try to start sending any sort of actual data over the line, we'll start by just sending a dummy value.
You'll want to use the "Simple" protocol in Peregrine.
Code:
void setup()
{
// First, we'll start serial running at 9600 bits per second.
Serial.begin(9600);
}
void loop()
{
// We print a dummy value out, terminated by
// the end-of-transmission character, a newline (\n)
Serial.print("1234.5\n");
// Now we wait a little bit so we don't overload Peregrine.
delay(50);
}
The graphing window will look like this:
Now, let's move to a changing value. We'll use a sine wave.
Our code now looks like this:
Code:
void setup()
{
// Again, we'll start by setting up the serial line.
Serial.begin(9600);
}
// We need a variable to store our angle to calculate the sine of.
// It's a floating point decimal.
float angle = 0.0;
void loop()
{
// Calculate a value
float outputVal = 1000.0 + 500.0 * sin(angle);
// Increment the angle for the next iteration of the loop function
angle += 0.01;
// Output the value over serial, letting Arduino handle output formatting
Serial.print(outputVal);
// Output a newline to denote the end of a transmission.
Serial.print("\n");
// Wait 50ms so as to not overload Peregrine.
delay(50);
}
Your output now looks something like this:
So there you have it - the most very basic aspects of getting an Arduino to send data to your computer.
In the next part, we'll discuss actually collecting data and transmitting it.
I'll try to get that written tonight. Until then, have fun tinkering.
Trevor
Last edited: