Pages: [1]   Go Down
Author Topic: 38khz Led Reciever Autonomous Robot  (Read 599 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 
Logged

Dubuque, Iowa, USA
Offline Offline
Edison Member
*
Karma: 45
Posts: 2469
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Post the code that you have now.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

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);
}
Logged

Dubuque, Iowa, USA
Offline Offline
Edison Member
*
Karma: 45
Posts: 2469
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this help? It worked for me:

http://learn.parallax.com/node/299
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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.

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

... here is some code to generate 38KHz on pin 9 of the Uno:

Code:
// 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.
« Last Edit: November 15, 2012, 09:14:02 am by dc42 » Logged

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: