element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Need Help on Wireless turn ON/OFF of LED using PIR sensors
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 4 replies
  • Subscribers 402 subscribers
  • Views 238 views
  • Users 0 members are here
Related

Need Help on Wireless turn ON/OFF of LED using PIR sensors

Former Member
Former Member over 13 years ago

Hi experts!

 

 

Im a newbie in using arduino and Im currently working on a simple project. I would like to automate my room lights using motion sensor, wirelessly. I need help on making LED turn ON and OFF using PIR motion sensor wirelessly, that is, i will be using two arduino uno and two xbee pro for the transmitter and receiver side. Im having problems on making my project work. It seemed that the receiver side is not receiveing the same data that i transmitted on the transmitter side. I hope you guys can help me. Here are my codes:

 

For the TX side:

 

int motionPin =0;

void setup()

{

  Serial.begin(9600);

  pinMode(motionPin, INPUT);

}

void loop()

{

  int val = analogRead(motionPin);

  Serial.println(val);

  delay(50);

}

 

For the RX side:

 

int ledPin = 10;

int lastDist = 0;

int currentDist = 0;

int thresh = 200;

void setup()

{

  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);

}

void loop()

{

  while(Serial.available() == 0);

  int currentDist = Serial.read();

  Serial.println(currentDist);

  if (currentDist < lastDist + thresh || currentDist > lastDist - thresh)

  {

    digitalWrite(ledPin, HIGH);

    delay(1500);

  }

  else

  {

    digitalWrite(ledPin, LOW);

  }

  lastDist = currentDist;

}

  • Sign in to reply
  • Cancel

Top Replies

  • chgus
    chgus over 13 years ago +1
    I have looked at your problem and noted a few points. 1) Your tx-side transmits data with 50ms delays. 2) Your rx-side reads data and then sleep 1500ms if triggered. I think You need to sync your read…
  • chgus
    chgus over 13 years ago

    I have looked at your problem and noted a few points.

     

    1) Your tx-side transmits data with 50ms delays.

    2) Your rx-side reads data and then sleep 1500ms if triggered.

    I think You need to sync your read to get a full and complete reading.

     

    3) rx-side Serial.read() read one char only, while tx side Serial.println() output a string, several chars.

    If you plug the example code from the Serial.read() doc page into your rx-side and work from there I

    think it might be clearer.

     

    Tip, there are new serial methods available now:

    * Look at Serial.readBytesUntil() to syncronize tx and rx, look for a /n, but a simple .available() + .read() loop will do just as well.

    * Look at Serial.parseInt() to read integer in serial rx-stream.

     

    I have no xbee experience so i hope my input still might be useful.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • ntewinkel
    ntewinkel over 13 years ago

    I think Christer nailed the issue - Serial.println sends the value as a string.

     

    So you could do some work to encode and decode the values so it comes out properly on the RX side.

     

    Or you can do all the work involving distance and thresholds on the TX side, and then use Serial.print to just send a 0 or 1 (or any single character for on/off).

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 13 years ago in reply to chgus

    Thanks for the reply experts! But i still couldn't figure a way to do it. Is it okay if i ask for an example or something? image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • chgus
    chgus over 13 years ago in reply to Former Member

    I'll try to put something together later tonight when I get home.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube