A project that i was developing for a workshop about electronics is a LEGO® train controled by an arduino. The project it’s explained in here. There were two situations where it was necessary to detect the train’s position. When this is was arriving to the station and when is was on an external track. First i’ve tried to use “Line Tracking” sensors but didn’t have enough range. On the second try used a Keyes IR Obstacle Sensor (this model) and they worked.

Keyes IR Obstacle Sensor
Keyes IR Obstacle Sensor

These sensors have four pins:

  • Ground;
  • 5V;
  • Communication;
  • a fourth pin that s deactivated;

The communication it’s very simple. When the sensor doesn’t detect an object in range returns “1”. If the sensor detects an object it returns “0”.

Code:

You can use the folowing code for testing.

void setup()
{
 Serial.begin(9600);
 pinMode( 12, INPUT);
}
void loop()
{
 Serial.print("Reading:");
 Serial.println(DigitalRead(12));
 delay(1000);
}

Write A Comment


The reCAPTCHA verification period has expired. Please reload the page.