Go Down

Topic: 38khz Led Reciever Autonomous Robot (Read 713 times) previous topic - next topic

earthswater

Anyone have any code to make an obstacle avoidance sensor for the Uno.  I have IR leds and TSOP4838 sensors.  I need to know how to pulse the LEDs at 38khz and then pick up that signal on the sensors when the light reflects off an object.  I have found various bits of code on the matter online but have had no luck.  Here is what I have tried with no luck:  http://letsmakerobots.com/node/29634?page=2    and http://www.instructables.com/id/DIY-Infrared-Proximity-Sensor-Arduino-Compatible/#step1    I am confident that I am assembling it correctly I just don't understand why it isn't working.  Thanks. 

Chagrin


earthswater

Code: [Select]


int IRsensorPin = 9;
int IRledPin = 10;
int ledPin = 13;
// #define IRsensorPin 9
// #define IRledPin 10
// #define D13ledPin 13

void IR38Write() {
  for(int i = 0; i <= 5; i++) {
    digitalWrite(IRledPin, HIGH);
    delayMicroseconds(13);
    digitalWrite(IRledPin, LOW);
    delayMicroseconds(13);
  }
}

void setup(){
  pinMode(IRsensorPin, INPUT);
  pinMode(IRledPin, OUTPUT);
  digitalWrite(IRledPin, LOW);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

void loop(){
  IR38Write();
  if (digitalRead(IRsensorPin)==HIGH){
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
  delay(20);
}

Chagrin

Code: [Select]
void IR38Write() {
  for(int i = 0; i <= 5; i++) {
    digitalWrite(IRledPin, HIGH);
    delayMicroseconds(13);
    digitalWrite(IRledPin, LOW);
    delayMicroseconds(13);
  }
}


It's only a series of 5 blinks; the IR receiver requires 6 minimum. The "letsmakerobots" example code -- which you should probably follow -- uses 384.

Nick Gammon

Does this help? It worked for me:

http://learn.parallax.com/node/299
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

dc42

I would use timer/counter 1 on the Uno to generate the 38KHz signal, if you are not already using it for something else. Then you only need to turn the signal on and off, instead of having to sit in a loop generating it.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

dc42

#6
Nov 15, 2012, 12:22 pm Last Edit: Nov 15, 2012, 03:14 pm by dc42 Reason: 1
... here is some code to generate 38KHz on pin 9 of the Uno:

Code: [Select]

// Generate a square wave of a given frequency on the OCR1A pin

#define REQUIRED_FREQUENCY  (38000)
#define REQUIRED_DIVISOR ((F_CPU/REQUIRED_FREQUENCY)/2)

#if (REQUIRED_DIVISOR < 65536)
# define PRESCALER  (1)
# define PRESCALER_BITS  (1)
#elif (REQUIRED_DIVISOR < 8 * 65536)
# define PRESCALER  (8)
# define PRESCALER_BITS  (2)
#elif (REQUIRED_DIVISOR < 64 * 65536)
# define PRESCALER  (8)
# define PRESCALER_BITS  (3)
#elif (REQUIRED_DIVISOR < 256 * 65536)
# define PRESCALER  (8)
# define PRESCALER_BITS  (4)
#elif (REQUIRED_DIVISOR < 1024 * 65536)
# define PRESCALER  (8)
# define PRESCALER_BITS  (5)
#else
# error Bad frequency
#endif

# define TOP        (((REQUIRED_DIVISOR + (PRESCALER/2))/PRESCALER) - 1)

void setup()
{
 pinMode(9, OUTPUT);
 digitalWrite(9, LOW);
 TCCR1A = 0;
 TCCR1B = (1 << WGM12) | PRESCALER_BITS;    // turn on
 TCCR1C = 0;
 OCR1AH = (TOP >> 8);
 OCR1AL = (TOP & 0xFF);
}

void on()
{
 TCNT1H = 0;
 TCNT1L = 0;  
 TCCR1A = (1 << COM1A0);
}

void off()
{
 TCCR1A = 0;
}

void loop()
{
 // Generate a burst 2ms long, then wait 10ms before generating the next one
 on();
 delay(2);  
 off();
 delay(10);
}


Pin 12 10 will not be available for PWM.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

earthswater

Okay thank you all, I will try all the suggestions tonight and get back to you

Go Up