Mbed Using Sensors

From Imperial College Robotics Society
Jump to: navigation, search


This will vary slightly from sensor to sensor. However, most sensors will have a power and ground wire which you can attach directly to the common ground and power outputs of your circuit. At least one of the wires will undoubtedly go into one of the pins of the mbed. Refer to the quick reference card shown bellow.

Mbed quick reference.png

Digital inputs can be wired to almost any of the pins except for the power management ones in red. However, this limits these sensors to an on or off input. Many sensors will use analogue inputs e.g. IR sensors use analogue to provide information on distance. Analogue sensors can only be attached to pins 15 to 20. Using a breadboard one would attach the mbed in the centre so that it bridges the gap. The rails to the sides can then be used to provide power and ground. To attach a wire to an mbed input you would simply strip the end of it, to expose the inner conductor, and then stick it into the rail travelling out horizontally to the mbed.


Please see the C++ tutorial and the Mbed Compiler if you are unfamiliar with how to program the mbed.

The mbeds online compiler contains commands that allow you to setup different pins and read from them. To setup a pun you must specify the pin type, give it a name and say which pin number it is. Once you setup a pin you can simply read it as if it were a variable, as shown below.

  1. #include "mbed.h"
  3. DigitalIn input(p5);  //sets up pin 5 to be a digital pin that we have called "input"
  4.  // Digital pins are set to 1 if on and 0 if  off.
  6. DigitalOut led(LED1); //just an output led
  8. int main(){
  9.     while(1){
  11.         if (input==1){    //we can read and test this pins as if it were a normal variable.
  12.            led=0;
  13.         }else{
  14.            led=1;
  15.         }
  17.     }
  18. }
  19. }

Digital pins are easy to work with as they will be set to 1 if on an 0 if off. This allows you to use a simple switch to control aspects of your robot or use touch sensors to detect when it has collided with a wall. Analogue inputs can take any value between 0 and 1. These are very useful for IR sensors as you can check the distance to an object infront of the robot. The easiest way to use this would be to simply do a certain action when the value passes a certain threshold but one could create functions to do this instead. Both examples are shown below.

  1. #include "mbed.h"
  3. AnalogIn dist(p15);  //sets up pin 15 to be a distance input for an analogue sensor
  5. DigitalOut led(LED1); //just an output led
  7. PwmOut motor(p21);   //sets pin 21 to be the output for a motor using Pulse Width Modulation
  9. int main(){
  10.     while(1){
  12.         if (dist>0.5){    //we can conduct actions when the analogue input passes a certain threshold
  13.            led=0;
  14.         }else{
  15.            led=1;
  16.         }
  18.         motor=(1-dist)*0.5  //we can also use the analogue input in functions for smoother control.
  20.     }
  21. }
  22. }

When using analogue inputs one must be wary of noise which always accompany the reading from the pin. There are various other, more in-depth ways to control the sensor inputs which are not covered here.