GoRobotics - Robotics news, robot projects

Archive for the ‘Articles’ Category

Arduino 5 Minute Tutorials: Lesson 3 – Potentiometer

Posted on April 19th, 2012 in Microcontrollers

Lessons Menu:

Lesson 3 Hardware:
  1. Computer / Laptop or Netbook
  2. Arduino Microcontroller
  3. USB to Serial Adapter (if your microcontroller does not have a USB port)
  4. Appropriate USB cable (Arduino boards draw power from the USB port – no batteries yet)
  5. Potentiometer (rotary or linear) Example: DFRobot Rotation Sensor
  6. Optional secondary LED.

Open the sample sketch “AnalogInput” under File -> Examples -> Analog. The comments section has been reduced below to make the code clearly visible.

We see several new lines of code here:

int sensorPin = A0;

The “int” is short for “integer”. The name “sensorPin” was chosen only to describe what the variable represents; “sensor pin”. The fact that the ‘P’ is capitalized makes it easier to see that it is actually two words, since spaces cannot be used. The integer “sensor pin” is equal to A0, where “A0″ is Analog pin 0 on the Arduino. On its own, “A0″ is not a reserved term. However, when used in context, the system recognizes it as analog pin 0. The line must be ended with a semicolon. By declaring a variable in the setup, you can use the term, which in this case is “sensorPin”, throughout the code instead of “A0″. There are two main benefits to this:

1) It makes the code more descriptive

2) If you want to change the value of the variable, you only need to do it in one place.

sensorValue = analogRead(sensorPin);

This line uses the term “analogRead” in order to read the voltage of an analog pin. Most Arduino microcontrollers use 10 bit analog (voltage) to digital (numeric) conversion, which is 210 possible numbers = 1024. Therefore a voltage of 0V corresponds to a numeric value of 0. A voltage of 5V corresponds to a numeric value of 1024. Therefore a value of 3V would correspond to a numeric value of:

3/5 =x/1024, x = 3*1024/5 = ~614

Alternatively you could have written: sensorValue = analogRead(A0);

int ledPin = 13;

Once again, the term “ledPin” is not a reserved word in Arduino, it was chosen to describe which pin was connected to the LED. The value “13″ is a normal value, but just like “A0″, when used in context represents pin 13.

int sensorValue = 0;

The term “sensorValue” is not a reserved term either.

Connect the potentiometer to pins A0, 5V and GND. The middle (wiper) lead is the one to connect to the analog pin and the voltage varies on this pin. The orientation of the other two pins does not matter. The other option is to connect the potentiometer to pins A0, A1 and A2. However, you will need to add the following code under void setup():

digitalWrite (A1, LOW);

digitalWrite (A2, HIGH);

This sets the corresponding pins to 0V (GND) and 5V (PWR). Once the potentiometer is connected, upload this sketch to the board and change to the Serial monitor. As you rotate the knob (or slide the slider), the values should change between 0 to 1023. Correspondingly, the LED will blink with a faster or shorter delay.

You can now read values and use them within your code. The new function used here is “analogRead();” where the pin selected is pin #2. If you used analog pin #5, you would change the code to read:

int sensorpin = 5;

If the system does not work, check the syntax and ensure the code uploads correctly. Next, check the connections to the potentiometer ensuring that the middle lead goes to the correct pin, and the other pins are powered at 0V and 5V. If you bought a very cheap or old potentiometer, there is a chance it may be mechanically defective. You can test this using a multimeter and connect the ends to the middle pin and an outer pin. Set the multimeter to read resistance and rotate the knob; if the resistance changes slowly, the pot is working. If the resistance is erratic, you need a new potentiometer.

Now what if you want to see the value yourself? Take a look at the code above and write it in the Arduino interface as a new sketch. Some new code you will see is:

Serial.println(sensorValue);

This sends the value contained in the variable “sensorValue” serially via the USB plug and digital pin 1. Verify, then upload this sketch to your Arduino. Once it is done, press on the “magnifying glass” located towards the top right of the window. This is the “Serial monitor” and monitors communications being sent and received by the Arduino. Here you must verify that the Baud rate is also 9600, or else you will see garbage.

If you did not want the values to appear on a new line every time, you could write

Serial.print(sensorValue);

Arduino 5 Minute Tutorials: Lesson 2 – Basic Code & Blink LED

Posted on April 17th, 2012 in Microcontrollers

Lessons Menu:

Lesson 2 Hardware:

  1. Computer / Laptop or Netbook
  2. Arduino Microcontroller
  3. USB to Serial Adapter (if your microcontroller does not have a USB port)
  4. Appropriate USB cable (Arduino boards draw power from the USB port – no batteries yet)

The Arduino language is CASE SENSITIVE: a capital letter is not the same as a lower case letter. The following code represents the minimum in order for a program to compile:

Arduino 5 Minute Tutorials

The “void setup()” section is widely used to initialize variables, pin modes, set the serial baud rate and related. The software only goes though the section once.

The “void loop()” section is the part of the code that loops back onto itself and is the main part of the code. In the Arduino examples, this is called “Bare Minimum” under File-> Examples -> Basics. Note that you are free to add subroutines using the same syntax:

void subroutinename() {}

Almost every line of code needs to end with a semicolon ‘;’ (there are a few exceptions which we will see later). To write single line comments in the code, type two back slashes followed by the text:

//comments are overlooked when compiling your program

To write multi-line comments, start the comment with /* and end with */

/* This is a multi-line comment and saves you having to always use double slashes at the beginning of every line. Comments are used used to explain the code textually. Good code always has a lot of comments.*/

Serial Communication

The Arduino board can communicate at various baud (“baud rates”). A baud is a measure of how many times the hardware can send 0s and 1s in a second. The baud rate must be set properly for the board to convert incoming and outgoing information to useful data. If your receiver is expecting to communicate at a baud rate of 2400, but your transmitter is transmitting at a different rate (for example 9600), the data you get will not make sense. To set the baud rate, use the following code:

void setup() {

Serial.begin(9600);

}

9600 is a good baud rate to start with. Other standard baud rates available on most Arduino modules include: 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200 and you are free to specify other baud rates. To output a value in the Arduino window, consider the following program:

Arduino 5 Minute Tutorials

Verify the program by pressing the “verify” button (looks like a “play” button in order version or a check sign in Arduino 1.0); you should not get any errors at the bottom of the screen. If you get errors, check that only the two numbers in the code are black, the rest of the text should have been automatically recognized and assigned a color. If part of the text is black, check the syntax (often copy/pasting text from another program can include unwanted formatting) and capitalization.

Next, upload the sketch to the board using the “Upload to I/O Board” button (arrow pointing right). Wait until the sketch has finished uploading. You will not see anything unless you then select the “Serial Monitor” button (rectangle with a circle that looks like a TV in the old software, or what looks like a magnifying glass in the new software). When you select the serial monitor, make sure the baud rate selected is the same as in your program. If you want to save all your programs, we suggest creating a new folder called “reference” and save this program as Hello World.

Blink LED Program

Connect the board to the computer if it is not already connected. In the Arduino software go to File ->  Examples -> Basics -> Blink LED. The code will automatically load in the window, ready to be transferred to the Arduino. Ensure you have the right board chosen in Tools -> Board, and have the right COM port as well under Tools -> Serial Port. If you are not sure which COM port is connected to the Arduino, (on a Windows machine) go to Device Manager under COM & Ports.

Press the “Upload” button and wait until the program says “Done Uploading”. You should see the LED next to pin 13 start to blink. Note that there is already a green LED connected to most boards – you don’t necessarily need a separate LED.

Understanding Blink LED Code

From Lesson 2 you will recognize the basic code void setup(){} and void loop(){}. You will also recognize the green commented sections. The three new lines of code you have not seen before are:

pinMode(13, OUTPUT);

This sets pin 13 as an output pin. The opposite, being INPUT, would have the pin wait to read a 5V signal. Note that the ‘M’ is capitalized. A lower case ‘m’ would cause the word “pinmode” to not be recognized.

digitalWrite(13, HIGH); and digitalWrite(13, LOW);

The line digitalWrite(pin, HIGH); puts a specified pin high to +5V. In this case we chose pin 13 since on the Uno, the LED is connected to pin 13. Replacing HIGH with LOW, the pin is set to 0V. You can attach your own LED using a digital output and the GND pin. Note that the ‘W’ is capitalized.

delay(1000);

The delay(1000); line causes the program to wait for 1000 milliseconds before proceeding (where 1000 is just a convenient example to get a 1 second delay). Note that during a delay, the microcontroller simply waits and does not execute any additional lines of code.

Special Note

Pin 13 incorporates a resistor with the LED, whereas none of the other digital pins have this feature. If you want to connect one or more LEDs to the other digital pins, you need to add a resistor in series with the LED.

Arduino 5 Minute Tutorials: Lesson 1 – Software

Posted on April 11th, 2012 in Articles, Microcontrollers

Lessons Menu:

Lesson 1 Hardware:

  1. Computer / Laptop or Netbook
  2. Arduino Microcontroller
  3. USB to Serial Adapter (if your microcontroller does not have a USB port)
  4. Appropriate USB cable (Arduino boards draw power from the USB port – no batteries yet)

The popularity of Arduino is steadily increasing and it is fast becoming the microcontroller of choice for students, hobbyists and smaller companies. Many different electronics PCB manufacturing companies are jumping on the bandwagon and producing their own variations of the boards, as well as “shields” (additional circuits that fit directly on top of many Arduino boards to increase their functionality) and accessories. The Arduino website offers free resources and tutorials as well as a language reference to help you understand the code and syntax. In order to get started, you will at the very minimum need an Arduino board. Note that all the Arduino (and most of the clone boards) can use the Arduino software. If you are unsure what hardware to get, the Arduino USB is currently the most popular model, and these 5 minute tutorials are based around it.

Downloading / Installing Arduino Software

  1. Go to www.arduino.cc to download the latest version of the Arduino software (Direct link: http://arduino.cc/en/Main/Software and select your operating system; in this case we are using Windows)
  2. Save the ZIP file to your desktop (you can move or delete it later)
  3. It is convenient to create a new folder called “Arduino” under “Program Files”. To do this, go to “My computer” -> “C:” (or the drive where the operating system is installed) -> “Program Files”, then left click once on “program Files” folder, then select “New”->”Folder” from the main Explorer menu.
  4. Extract the entire ZIP folder to this new “Arduino” folder
  5. To run the Arduino software, open Windows Explorer by pressing the windows key (usually between the Ctrl and Alt keys on your keyboard) and the ‘E’ character at the same time (there are other ways to access explorer as well).
  6. Go to “My computer” -> “C:” (or the drive where the operating system is installed) -> “Program Files” -> “Arduino” In this folder you will see an executable file (blue colored icon named “Arduino”), you can left click (once) and then right click and select “send to” -> Desktop (create shortcut) to have Arduino more easily accessible.
  7. Double click the icon on the desktop to start the software.

The Arduino Software Interface

The Arduino interface is pretty “bare-bones”. When you load the software, the first screen you will see is a white window (shown below) with several different shades of blue and blue-green as border.  Arduino projects are called “sketches” and when you start a new sketch, several additional files are also created.

“Newest: Arduino Interface (as of Q1 2012)

The main headings are “File” “Edit” “Sketch” “Tools” “Help” and several shortcut icons beneath “Verify”, “Upload”, “New”, “Open”, “Save”, and at the far right, the “Serial Monitor”. Note that all these icons are also available from the main menus.

“Older” Arduino Interface

The main headings are “File” “Edit” “Sketch” “Tools” “Help” and several shortcut icons beneath “Verify”, “Stop”, “New”, “Open”, “Save”, “Upload” and “Serial Monitor”. Note that all these icons are also available from the main menus.

Arduino 5 Minute Tutorials

To connect to your board,
  1. Launch the Arduino software by double-clicking the Arduino icon
  2. Plug one end of the USB into the Arduino and the other end into your computer.
  3. Your computer should detect the new device and tell you if it has installed correctly. At this time, two things can happen; if you have an older board using an FTDI chip (ex. Duemilanove based), Windows should detect it and you’re good to go to the next step. If you have a board which uses an ATMega chip to convert USB to serial (for example the UNO), you will need to install the drivers manually.
  4. Take a look at your board’s main processor chip (usually found between the pin headers) to see which you have. It will likely be the ATMega168, ATMega328, or a more powerful ATMEga640. ATMega1280 etc
  5. In the software, select “Tools” -> “Board” -> You will get a list of possible boards. If you have a different board, select it from the drop-down list; if you have purchased a compatible board, that manufacturer should indicate which board to choose.
  6.  In the software, select “Tools” -> “Serial Port” -> COM # (note that if you have several COM ports, you will need to go to Device Manager to see which COM port is assigned to your board.
You’re now ready to start coding!  

History of Robotics

Posted on April 9th, 2012 in Latest News, Miscellaneous, News

This section is intended to provide you with an overview of the history of robotics. As you may have guessed, the history of robotics is intertwined with the history of technology, science and the basic principle of progress. Technology used in computing, electricity, even pneumatics and hydraulics can all be considered a part of the history of robotics. Robotics currently represents one of mankind’s greatest accomplishments and is the single greatest attempt of mankind to produce an artificial, sentient being.

Timeline

The focus of this timeline is to provide the reader with a general overview of robotics and to give an appreciation for the inventors and innovators in this field who have helped robotics to become what it is today. Click the link to see the entire timeline. See something important that’s missing? Write it in the comments below and we’ll be happy to consider it in the next revision.

Articles

How Does Wheel Size Affect My Robot?

Posted on March 2nd, 2012 in Mechanics

What Size Wheel Should I Use on My Mobile Robot?

Are you looking to make a wheeled robot and not sure what size wheel you need? Here we provide an insight into some of the equations which affect your robot’s speed and distance traveled.

Angular Velocity

The velocity (feet per second) of a robot is directly related to the angular velocity, w (in radians per second – NOT RPM) of the motor and the radius of the wheel, R (ideally in meters or feet, so when calculating v it is in meters per second or feet per second).

w = v / R

Re-arranging this equation, we find the velocity of the robot:

v = w*R

Most motor manufacturers provide the no load RPM, so to convert RPM to radians per second, you need the following equation:

1 rev/min = 2*pi/60 rad/sec

Often, a “Voltage constant”, Kv is provided by the manufacturer; for example, a motor may be rated at Kv= 2.4 Volts / 1000 RPM which correlates the RPM to the voltage.

This is the theory, now the reality: It’s not always possible to operate a motor at its nominal voltage, and we often need to operate it at a few volts higher or lower. Keep in mind that the “nominal” voltage runs the motor at near peak efficiency, which is still never 100%; and as you move away from the nominal voltage, the efficiency drops, so the motor needs more current to provide the same torque, consuming the battery even faster. Next, keep in mind that not all motors can be operated at all voltages and all motors have a maximum rated voltage.  The Voltage constant usually applies over a small range around the nominal voltage, and outside this range, the curve is not linear and not only will the motor not be operating efficiently, the output will not be anything like what you predict with the equations.

Distance Traveled

To get an idea of how far your robot has traveled, you need to know the circumference of the drive wheel:

Circumference (C) = 2*pi*R

Distance traveled = w*C*t (where ‘w’ is in rad/sec and ‘t’ is in seconds) So what to do? Here are some useful tips:
  • Choose smaller sized wheel for flat terrain, larger wheel for more “off-road” (or large obstacles)
  • Proportional to the size of the robot (choosing a wheel proportional to the size of the robot will usually mean the motor you will need is also proportional, and the robot has a better chance of moving at a decent speed)
  • See what wheels are out there – not all wheels have mounting hubs designed for all motors
  • Keep in mind that a larger wheel may require a more powerful motor; we’ll go into more detail about this in a future article.
  • Use the same wheel for all drive motors: as you can see, changing the radius affects both the speed and the distance traveled: you don’t want one wheel “fighting” against the other.
 
Subscribe to the RobotShop Blogs RSS feed!

Enter your email address: