Carlotta Berry
Published © GPL3+

Lily∞Bot with Arduino Uno: Testing Infrared sensors

In this project, infrared sensors are attached to the open-source robot Lily∞Bot to use for obstacle avoidance, object sensing.

IntermediateFull instructions provided3 hours76
Lily∞Bot with Arduino Uno: Testing Infrared sensors

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE
Arduino Web Editor
Arduino Web Editor

Hand tools and fabrication machines

Multitool, Screwdriver
Multitool, Screwdriver
3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

GITHUB repository

Schematics

IR Wiring on Lily∞Bot

Lily IR Wiring Fritzing File

Code

Short range IR code

Arduino
int ylwLED, redLED, bluLED, grnLED;
float val, distance, volts;

// read sonar and return distance

int get_distance2() {
  val = (analogRead(A0));
  volts = val * 5;
  volts = volts / 1024;
  distance = 568.57 * pow(volts, -0.995);
  distance = distance / 25.4;
  return distance;
}

// turn of all LEDs
void all_LEDS_off2() {
  digitalWrite(bluLED, LOW);
  digitalWrite(grnLED, LOW);
  digitalWrite(redLED, LOW);
  digitalWrite(ylwLED, LOW);
}

void setup() {
  pinMode(ylwLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(bluLED, OUTPUT);
  pinMode(grnLED, OUTPUT);
  ylwLED = 10;
  redLED = 11;
  bluLED = 12;
  grnLED = 13;
  pinMode(A0, INPUT);
}

void loop() {
  if ((get_distance2()) < 9) {
    digitalWrite(ylwLED, HIGH);
  } else if ((get_distance2()) < 10) {
    digitalWrite(redLED, HIGH);
  } else if ((get_distance2()) < 11) {
    digitalWrite(bluLED, HIGH);
  } else {
    digitalWrite(grnLED, HIGH);
  }
  delay(100);
  all_LEDS_off2();
  delay(100);
}

Long Range IR Code

Arduino
int ylwLED, redLED, bluLED, grnLED;
float val, distance, volts;

// read sonar and return distance

int get_distance2() {
  val = (analogRead(A0));
  volts = val * 5;
  volts = volts / 1024;
  distance = 29.988 * pow(volts, -1.173);
  distance = distance / 2.54;
  return distance;
}

// turn of all LEDs
void all_LEDS_off2() {
  digitalWrite(bluLED, LOW);
  digitalWrite(grnLED, LOW);
  digitalWrite(redLED, LOW);
  digitalWrite(ylwLED, LOW);
}

void setup() {
  pinMode(ylwLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(bluLED, OUTPUT);
  pinMode(grnLED, OUTPUT);
  ylwLED = 10;
  redLED = 11;
  bluLED = 12;
  grnLED = 13;
  pinMode(A0, INPUT);
}

void loop() {
  if ((get_distance2()) < 4) {
    digitalWrite(ylwLED, HIGH);
  } else if ((get_distance2()) < 6) {
    digitalWrite(redLED, HIGH);
  } else if ((get_distance2()) < 8) {
    digitalWrite(bluLED, HIGH);
  } else {
    digitalWrite(grnLED, HIGH);
  }
  delay(100);
  all_LEDS_off2();
  delay(100);
}

Lily IR Code

Arduino
/*
  LilyBot
  LilyBot-Sharp IR.ino
  This program will test that the LEDs are working on the controller

  Carlotta A. Berry, PhD
  September 4, 2022
  These type of distance sensors tend to be a bit noisy, so it is recommended to add a capacitor between Vcc and GND.
  The datasheet suggests a capacitor of 10 F or more (I used 220 F). Connect the positive lead of the capacitor to
  the Vcc wire connection and the negative lead to the GND wire connection (see picture). Capacitors are often marked
  with a stripe which indicates the negative lead. The positive lead is often longer then the negative lead.
  Reference: https://www.makerguides.com/sharp-gp2y0a21yk0f-ir-distance-sensor-arduino-tutorial/

  Hardware Connections:
  Vmotor - voltage for the motors, not logic level (4.5-13.5V)
  Vcc - voltage for the logic levels (Arduinos, 5V)
  GND - shared logic and motor ground

  Distance (cm) = 29.988 X POW(Volt , -1.173)

  Sharp GP2Y0A21YK0F IR Connections (10 - 80 cm range) = 4 inches to 32 inches
  front A0
  back A1
  left A2
  right A3

  Yellow LED pin 7
  Red LED pin 6
  Green LED pin 5
  Blue LED pin 4
*/

//state LEDs
int ledPins[4] = {10, 11, 12, 13};
int ylwLED = 10;
int redLED = 11;
int grnLED = 12;
int bluLED = 13;


/********************************************************************************/
void setup()
{
  int waittime = 1000;               //robot wait time
  int baudrate = 9600;              //serial communication baud rate
  for (int i = 0; i < 4 ; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  allLEDSon();
  delay(100);
  allLEDSoff();
  delay(100);
  Serial.begin(baudrate);           //start serial commnication
  Serial.print("Lilybot begin");  //print start message

}

/********************--**********************************************************/
void loop()
{
  if ((readIR()) < 6) {
    digitalWrite(ylwLED, HIGH);
    Serial.println("yellow");
  } else if (readIR() < 8) {
    digitalWrite(redLED, HIGH);
    Serial.println("red");
  } else if (readIR() < 10) {
    digitalWrite(bluLED, HIGH);
    Serial.println("blue");
  } else {
    digitalWrite(grnLED, HIGH);
    Serial.println("green");
  }
  delay(100);
  allLEDSoff();
  delay(100);
}


//turn all the LEDS off
void allLEDSoff () {
  for (int i = 0; i < 4 ; i++) {
    digitalWrite(ledPins[i], LOW);
  }
}

// turn ON all LEDs
void allLEDSon() {
  for (int i = 0; i < 4 ; i++) {
    digitalWrite(ledPins[i], HIGH);
  }
}

float readIR() {
  float val = analogRead(A0);
  float volts = 5 * val / 1024;

  //  Serial.print("volts: ");
  //  Serial.println(volts);

  float dist_cm = 29.988 * pow(volts, -1.173);
  float dist_in = dist_cm / 2.54;
  //  Serial.print("cm: ");
  //  Serial.print(dist_cm);
  //  Serial.print("\tin: ");
  //  Serial.println(dist_in);
  return dist_in;
}

Credits

Carlotta Berry
14 projects • 22 followers
Carlotta Berry is a Professor and Dr. Lawrence J. Giacoletto Endowed Chair for Electrical and Computer Engineering at Rose-Hulman.
Contact

Comments

Please log in or sign up to comment.