Tuesday, March 1, 2011

Getting Started

So I've been getting into electronics and programing again. This time i really wanted to learn what i could do with programing hardware.  After digging around on the internet to see how this was done, I ran across a website with a very easy sounding and detailed tutorial on how to get started programing a microchip on a board designed to do just that.  I eagerly ordered a Arduino Starter Pack and a LCD from adafruit.com and eagerly waited for its arrival.  Once i got it, I dove though the tutorial and completed it in just a few hours.  It was probably the most fun I've ever had learning...period.  But, like a fat kid with a pack of twinkies, the second twinkie was gone before I knew it, leaving me holding an empty wrapper.  Then, after cruising the internet for project ideas for a while, I came up with my first project, designed and implemented all on my own.  Before i get to that though, what is an Arduino actually?

What is an Arduino?
from the Arduino.cc website:
Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.


Arduino can sense the environment by receiving input from a variety of sensors and can affect its surroundings by controlling lights, motors, and other actuators. The microcontroller on the board is programmed using the Arduino programming language  and the Arduino development environment. 

Arduino projects can be stand-alone or they can communicate with software on running on a computer.  The boards can be built by hand or purchased preassembled; the software can be downloaded for free.

Sounds neat huh?  For me, it sounded like the perfect gateway to dive deeper into the realm electronics by merging my love of hardware with the software side of things.  I've always had a head for programing, but I haven't been very productive in a classroom environment.  Plus i'm a perfectionist with my code which makes debugging and optimizing code take forever which isn't good for deadlines.  Doing things on my own, really lets me take my time, come back to things to tweak things as more ideas come to me.  It really is a liberating feeling.

First Project

For my first project I looked though what I had available in my kit:


The Main kit from Adafruit.com includes:
  • Arduino Uno SMD w/Atmega328 - The latest and greatest Arduino revision, assembled and ready to go, including 4 rubber feet
  • 3' USB cable - Perfect for connecting your Arduino to a computer
  • Protoshield Kit - One of ladyada's designs, its got everything you need to make prototype designs using an Arduino. Note that this comes unassembled
  • Tiny Breadboard - Fits on top of the protoshield, easy to use
  • 9V DC regulated wall adapter - You can power your Arduino from any wall socket. This switching regulator is efficient and small and works with US (110V) and European (220V) power.
  • 9V Battery case with switch and a 2.1mm plug- so you can power your arduino using a 9V battery. This case is much sturdier than just a battery clip and it has an on/off switch too! Note that this comes unassembled
  • Tutorial starter pack parts - Includes a 10K potentiometer1K potentiometer2 small pushbuttons5 red diffused bright LEDs, one each of red, green and blue ultra-bright LED5 100 ohm resistors5 1K resistors5 10K resistors, and a CdS photocell sensor.
  • Also includes 75 flexible breadboard wires in 8 colors, perfect for use with the solderless breadboard.
Plus i also perchaced a 16x2 Red and Black LCD.  The 16x2 stands for the numbers of character rows and column.  In this case, Two rows of sixteen characters.



After playing around with the LCD tutorial and getting simple outputs, I then started playing with the photocell sensor.  Simply, a photocell is a resistor (resistors being devices that "resist" the flow of electricity), whose resistance decreases the brighter things get.  After playing with the tutorials, i wondered if i could combine the two on my own and customize the output into something more unique.  So thats what i did, i wired up the two devices just like in the tutorial.

Then I merged the code and made some modifications based on what I wanted as an output.  Here is an example of the code I used.
/*
Light Sensor!
Reads Analog data from a light sensor and outputs the data in a form we can understand
*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int photocellPin = 0; // the cell and 10K pulldown are connected to analog pin 0
int photocellReading; // the analog reading from the sensor divider

void setup(void) {
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
}

void loop(void) {
lcd.print("It is currently:"); // Prints the defult message to the LCD.
lcd.setCursor(0, 1); // changes cursor position
photocellReading = analogRead(photocellPin); // dumps the analog data into my varable
if (photocellReading < 5) { // start of if else statments to covert the analog intergers to text and output both.
lcd.print("Dark-");
lcd.print(photocellReading);
} else if (photocellReading < 20) {
lcd.print("Dim-");
lcd.print(photocellReading);
} else if (photocellReading < 100) {
lcd.print("Light-");
lcd.print(photocellReading);
} else if (photocellReading < 200) {
lcd.print("Bright-");
lcd.print(photocellReading);
} else {
lcd.print("Very bright-");
lcd.print(photocellReading);
}
delay(1000); // delays 1sec
lcd.clear(); // clears the screen, acting as a refresh
}
This is what i ended up getting.  (note: the picture was taken before the final version of the code was done.)

Basically whats going on is the photocell  is giving us a integer value via the analog input on the Arduino board. The Arduino board then looks at that integer and asks a few questions.  Is the number less the 5? If yes, print out the the string of text "Dark-" then print the number.  If not, is the number less then 20?  And so on.  Then the program waits one second to clear the screen and then repeats itself.

Im kinda proud of myself on this one.  I borrowed some code (the if/else portion) but the rest I did from memory or by looking up the language reference.  Since doing this, I've come up with three new projects ideas but i'll explain them in my next post.

http://burntelectonics.blogspot.com/2011/02/getting-started.html

No comments:

Post a Comment