Gorobotics is the perfect blog for everyone looking to learn how to make a robot, gather knowledge about DIY robotics projects, and stay in touch with the latest robotics news.
Force “sensors” are actually “force sensing resistors” (FSRs). Similarly, bend “sensors” are actually products whose resistance changes with flexing. These can all be categorized as “variable resistors”. To interface a product whose resistance changes with a microcontroller, you need a voltage divider circuit. This “circuit” is nothing complex – aside from wires, the only part you are missing is a resistor.
To create the circuit, add the variable resistor in series with a similar (standard) resistor of roughly the same resistance (in ohms). Connect a wire between the two – this wire goes to the analog input of the board. There should only be two wires left – one end of the standard resistor, and one end from the variable resistor – these ends are connected to +5V and GND respectively. You can now use it as a regular sensor with analog output.
The output of this “mini circuit” is a signal between 0 to 5V (this is referred to as an analog signal), which is connected to an analog pin of the microcontroller. The microcontroller’s on-board analog to digital converter (ADC) interprets this voltage and assigns it a number which you can use in your code. For 10 bit ADC (210), you will get a number between 0 and 1024 representing 0V to 5V. You would need an equation in your code to use this number to send the appropriate signal to a motor controller. As you might have suspected, the code is now identical to that used to get an analog input.
To get sample code, open the Arduino software and go to File -> Examples -> Analog -> AnalogInOutSerial
The video above shows a bend sensor connected to an Arduino, and the Arduino is connected to a small servo motor. The analog value associated with the flex sensor is read by the Arduino, and that value is converted to a rough position. You would merge the Analog example code with the servo code, and add a single line to convert the 0 to 1024 value to 0 to 180 degrees. It is easy to see how, with many of these sensors, you can create a data glove which controls a robotic hand.
Appropriate USB cable (Arduino boards draw power from the USB port – no batteries yet)
Standard servo motor (current consumption <50mA)
Pin headers / cables
Controlling a servo motor directly from the Arduino is quite easy. However, a servo motor may require significantly more current than the Arduino can provide. The following example uses a standard sized servo (without any load) powered directly from the Arduino via USB. When powering the servo directly from the Arduino board:
Connect the black wire from the servo to the GND pin on the Arduino
Connect the red wire from the servo to the +5V pin on the Arduino
Connect the yellow or white wire from the servo to a digital pin on the Arduino
Alternatively, you can plug the servo’s wire into three adjacent pins, and set the pin connected to the red lead to “HIGH” and the pin connected to the black lead to “LOW”. If you want to use a more powerful servo, or if you want to connect it to a separate power supply, you would connect the battery / power supply’s red (5V) and black (GND) wires to the servo’s red and black wires, and connect the signal wire to the Arduino. Note that you also need to connect the batter’s GND line to the Arduino’s GND pins (“common ground”).
pinMode(pin number, OUTPUT);
This sets a pin number as dedicated input or output. In this case, we called the pin “servopin” and assigned it a value of 4. The term “pulse” is in black as it is not a reserved word and can be changed by the user. It is best to use descriptive variables when coding to understand what each does, or the information it will contain. Servos operate by sending a timed +5V pulse (usually between 500us and 2500us) to the onboard electronics, which is repeated every ~20ms. This pulse corresponds to a servo position, usually from 0 to 180 degrees.
5V for 500 microseconds = 0.5 milliseconds and corresponds to 0 degrees
5V for 1500 microseconds = 1.5 milliseconds and corresponds to 90 degrees
5V for 2500 microseconds = 2.5 milliseconds and corresponds to 180 degrees
The relationship is linear, so use mathematics to determine the pulse which corresponds to a given angle. Note that if you send a signal that is greater or lower than the servo can accept (for example, Firgelli linear actuators accept 1 to 2 ms), you might damage the actuator.
Another option for controlling servos is to use the Arduino “servo library” (previously separate from the basic Arduino software, it is now included with V1.0). The servo library manages much of the overhead and includes new, custom commands. If you want to control multiple servo motors, you should use a servo motor controller and a separate power supply between 4.8V to 6V.
Push button and corresponding cables to connect to Arduino
Infrared distance sensors are useful for measuring distances without actually touching a surface. The three wires protruding from a distance sensor represent +5V (in most cases), Gnd (Ground) and signal. These are almost always color coded with black as ground, red as +V and white or yellow as the signal. If your infrared distance sensor did not come with any wires, you will either need to find the appropriate connector, or solder wires directly to the leads (ensure the pins and solder do not contact one another) so you can attach wires.
Connect the red wire to +5V on the Arduino
Connect the black wire to Gnd on the Arduino
Connect the yellow wire to an analog pin on the Arduino (in this case we chose A2)
Arduino 5 Minute Tutorials
Since the sensor is connected to the analog input of the Arduino, the code is identical to that of the potentiometer:
Upload this program to the board and change to the Serial Monitor. As you move the front of the distance sensor closer to and away from a solid object or wall, the values should change between 0 to 1023. You can now read values and use them within your code. Check the range for your sensor (not all sensors can read from zero cm); note that some sensors have a minimum distance – although it is always listed in the specifications, try to find it by experimentation. To convert the values to actual distances (in cm or inches), consult the user guide of the sensor.
Arduino and Push Buttons
Connecting toggle switches, push buttons and momentary contact switches to the Arduino is straightforward. A push button is a simple device that completes a circuit. One end of the button is connected to source, usually a low voltage (5V on the Arduino is ideal) and the other connected to the digital pin. When the switch is flipped, pressed or toggled, the circuit is either opened or closed. The digital pin simply returns if there is 5V or 0V. The code associated with this is:
digitalRead(pin);
In the following simple program, a push button is used to turn on the LED connected to pin 13. The line
digitalWrite(ledPin, status);
turns the ledPin (in this case assigned to digital pin 13) HIGH (1) or LOW (0) depending on the status variable. We initially set the status to be low (0).
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
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:
The “voidsetup()” 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 “voidloop()” 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:
voidsubroutinename() {}
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:
voidsetup() {
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:
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.