arduino basics_ 433 mhz rf module with arduino tutorial 2

Upload: habaoanh

Post on 02-Jun-2018

254 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    1/13

    n Arduino blog that follows the footsteps of an enthusiastic Arduino user, from the ground up. Level : Basic

    rduino Basics

    Home Arduino Basics Projects Page Fe edback Page

    27 June 2014

    433 MHz RF module with Arduino Tutorial 2

    n the previous project, we transmitted a signal wirelessly from one Arduino to another. It was there to helproubleshoot communication between the modules. It was important to start with a very short distance (1-2

    cm) and then move the RF modules further apart to test the range. The range can be extended by solderingan antenna to the module, or by experimenting with different voltage supplies to the modules (making sure tokeep within the voltage limits of the modules.)

    n this project - we aim to receive a signal from an RF remote. The remote that I am using is a MercatorRemote Controller for a Fan/Light. (Remote controller code is FRM94). It is important that you use a remote

    hat transmits at the same frequency as your receiver. In this case, my remote just happens to use arequency of 433MHz. I was able to receive RF signals from from a distance of about 30cm without anantenna (from my remote to the receiver).

    Project 2: RF Remote Copy

    Video

    Sea

    Search This Blog

    Chn Ngn ng

    c h tr bi Dch

    Translate

    Arduino Basics Projects P

    Arduino Basics YouTubeVideos

    Feedback Page

    Home

    Pages

    Subscribe

    Posts

    Comments

    powered by

    My Arduino Videos

    13 Lin kt khc Blog tip theo To Blog

    http://arduinobasics.blogspot.com.au/p/arduino-basics-youtube-videos.htmlhttp://arduinobasics.blogspot.com.au/p/feedback.htmlhttp://arduinobasics.blogspot.com.au/2014/06/433-mhz-rf-module-with-arduino-tutorial.htmlhttp://arduinobasics.blogspot.com.au/http://arduinobasics.blogspot.com.au/http://arduinobasics.blogspot.com.au/http://arduinobasics.blogspot.com.au/p/feedback.htmlhttp://arduinobasics.blogspot.com.au/p/arduino-basics-projects-page.htmlhttp://www.icstation.com/product_info.php?ref=25&products_id=1402&affiliate_banner_id=1http://arduinobasics.blogspot.com.au/p/arduino-basics-projects-page.htmlhttps://translate.google.com/http://www.youtube.com/
  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    2/13

    Here are the parts that you will need to carry out this project:

    1 x Arduino UNO or compatible board

    Breadboard

    Wires

    Mercator FRM94 Remote Controller for Fan/Light (Transmitter Model: TR107A)

    RF Module (433 Mhz) - Receiveror the 315 Mhz version

    You can quickly test your remote, by pressing one of the buttons in close proximity to the RF receiver (usinghe same sketch as in Project 1), and you should see the LED flicker on an off in response to the button

    press. If you don't see the LED flickering, then this project will not work for you.

    Here is a picture of the remote controller that I am using:

    Parts Required

    Remote Controller

    2014(9)

    November(1)

    October(1)

    September(2)

    August(1)

    July(2)

    June(2)

    433 MHz RFmodule with

    ArduinoTutorial 2

    433 MHz RFmodule with

    ArduinoTutorial 1

    2013(13)

    2012(11)

    2011(25)

    Blog Archive

    1,052,613

    Total Pageviews

    http://arduinobasics.blogspot.com.au/search?updated-min=2011-01-01T00:00:00%2B08:00&updated-max=2012-01-01T00:00:00%2B08:00&max-results=25http://arduinobasics.blogspot.com.au/2014_08_01_archive.htmlhttp://arduinobasics.blogspot.com.au/2014/06/433-mhz-rf-module-with-arduino-tutorial.htmlhttp://arduinobasics.blogspot.com.au/2014_07_01_archive.htmlhttp://www.mercator.com.au/http://arduinobasics.blogspot.com.au/2014_10_01_archive.htmlhttp://void%280%29/http://www.icstation.com/product_info.php?ref=25&products_id=1554&affiliate_banner_id=1http://arduinobasics.blogspot.com.au/2014/06/433-mhz-rf-module-with-arduino-tutorial_27.htmlhttp://void%280%29/http://void%280%29/http://void%280%29/http://void%280%29/http://arduinobasics.blogspot.com.au/search?updated-min=2012-01-01T00:00:00%2B08:00&updated-max=2013-01-01T00:00:00%2B08:00&max-results=11http://arduinobasics.blogspot.com.au/2014_11_01_archive.htmlhttp://arduinobasics.blogspot.com.au/search?updated-min=2013-01-01T00:00:00%2B08:00&updated-max=2014-01-01T00:00:00%2B08:00&max-results=13http://arduinobasics.blogspot.com.au/2014_09_01_archive.htmlhttp://arduinobasics.blogspot.com.au/2014_06_01_archive.htmlhttp://3.bp.blogspot.com/-wIEi6eh6CBQ/U6elSm_-O4I/AAAAAAAAA3U/tGUPHENPwR0/s1600/MercatorRemote_Inside3.jpghttp://void%280%29/http://arduinobasics.blogspot.com.au/2014/06/433-mhz-rf-module-with-arduino-tutorial.htmlhttp://void%280%29/http://void%280%29/http://www.icstation.com/product_info.php?ref=25&products_id=1402&affiliate_banner_id=1http://void%280%29/http://void%280%29/http://arduinobasics.blogspot.com.au/search?updated-min=2014-01-01T00:00:00%2B08:00&updated-max=2015-01-01T00:00:00%2B08:00&max-results=9http://3.bp.blogspot.com/-hX2IgmEId1M/U6ejlD1SQ8I/AAAAAAAAA3I/8yUzYCO5T-E/s1600/Mercator_Remote_FRM94.jpghttp://www.icstation.com/product_info.php?ref=25&products_id=3516&affiliate_banner_id=1
  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    3/13

    The following sketch will make the Arduino wait until a signal is detected from the remote (or other 433 MHzRF device). Once triggered, it will turn the LED ON, and start to collect and store the signal data into anarray.

    did my best to keep the signal reading section of the sketch free from other functions or interruptions.Theaim is to get the Arduino to focus on reading ONLY... and once the reading phase is complete, it will reporthe signal data to the Serial monitor. So you will need to have the Serial monitor open when you press theemote control button.

    The remote control signal will be made up of HIGH and LOW signals - which I will try to illustrate later in theutorial. But for now, all you need to know is that the Signal will alternate between HIGH and LOW signals,

    and that they can be different lengths.

    This sketch aims to identify how long each LOW and HIGH signal is (to make up the complete RF remotesignal). I have chosen to capture 500 data points(or 250 LOW/HIGH combinations).You may wish to increaseor decrease the dataSize variable to accomodate your specific RF signal. In my case, I only really needed300 data points, because there was a "flat" signal for the last 200 data points (characterised by 200epetitions of a LOW signal length of 0 and HIGH signal length of 255)

    -------------------------------------------------

    1

    23

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    3132

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    4647

    48

    /*

    RF Remote Capture sketchWritten by ScottC 24 Jun 2014

    Arduino IDE version 1.0.5

    Website: http://arduinobasics.blogspot.com

    Receiver: XY-MK-5V

    Description: Use Arduino to Receive RF Remote signal

    ------------------------------------------------------------- */

    constintdataSize = 500; //Arduino memory is limited (max=1700)

    bytestoredData[dataSize]; //Create an array to store the data

    #define ledPin 13 //Onboard LED = digital pin 13

    #define rfReceivePin A0 //RF Receiver data pin = Analog pin 0

    constunsignedintupperThreshold = 100; //upper threshold value

    constunsignedintlowerThreshold = 80; //lower threshold value

    intmaxSignalLength = 255; //Set the maximum length of the signal

    intdataCounter = 0; //Variable to measure the length of the signalunsignedlongstartTime=0; //Variable to record the start time

    unsignedlongendTime=0; //Variable to record the end time

    unsignedlongsignalDuration=0; //Variable to record signal reading time

    voidsetup(){

    Serial.begin(9600);

    pinMode(ledPin, OUTPUT);

    /* The following code will only run ONCE --------------

    ---Press the reset button on the Arduino to run again-- */

    while(analogRead(rfReceivePin)

  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    4/13

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    6364

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    7879

    80

    81

    82

    83

    84

    85

    86

    87

    while(analogRead(rfReceivePin)

  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    5/13

    After pressing the button on the RF remote, the data signal is printed to the Serial Monitor. You can copy thedata to a spreadsheet program for review. This is an example of the signal produced after pushing the buttonon the remote for turning the fan/light on.

    Results

    http://3.bp.blogspot.com/-xF0vSFF5Hdw/U6mlTvzmpsI/AAAAAAAAA6E/KNlhXtbmg5Q/s1600/433+MHz+RF+Project+2+Live+Sketch.JPGhttp://3.bp.blogspot.com/-OXeO0PBtv3M/U6RWA1_JYyI/AAAAAAAAAyc/W_esqMY3h1s/s1600/RF+Receiver+Table.gifhttp://3.bp.blogspot.com/-1O0eaVqjgOk/U6mlIfJB_oI/AAAAAAAAA4c/TmivvjAOTV8/s1600/LightOn_SerialMonitorReadings.jpg
  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    6/13

    The following code was produced from pushing the button responsible for turning the light off:

    The code sequence above may seem a bit random until you start graphing it. I grabbed the LOW column -and produced the following chart:

    The chart above is a bit messy - mainly because the timing is slightly out... in that sometimes it can squeezean extra read from a particular signal. But what is important to note here is that you can differentiate aLONG signal from a SHORT signal. I have drawn a couple of red dotted lines where I believe most of thereadings tend to sit. I then used a formula in the spreadsheet to calibrate the readings and make them a bitmore uniform. For example, if the length of the signal was greater than 4 analogReads, then I converted thisto 6. If it was less than 4 analogReads, then I converted it to 2. I used a frequency table to help decide on

    the cutoff value of 4, and just decided to pick the two values (2 for short, and 6 for long) based on thefrequency tables below. I could have chosen 5 as the LONG value, but there were more 6's overall.

    **The meaning of "frequency" in the following tables relate to the "number of times" a specific signal lengths recorded.

    http://2.bp.blogspot.com/-h7lsUzWkH58/U6mlJc4-uJI/AAAAAAAAA4k/roq_l5Hx5Xk/s1600/LightOff_SerialMonitorReadings.jpghttp://4.bp.blogspot.com/-VYdgh5DycPc/U6mlLC5L1iI/AAAAAAAAA40/xKWja_AK78Y/s1600/LightOn_LowSignalLengthChart_Raw.jpg
  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    7/13

    And this is the resulting chart:

    You will notice that the pattern is quite repetitive. I helped to identify the sections with vertical red lines (nearthe bottom of the chart). In other words, the signal produced by the remote is repeated 6 times. then did the same for the HIGH signal column and combined the two to create the following chart:

    http://2.bp.blogspot.com/-ReFeYTnfQUw/U6mlOKmBcaI/AAAAAAAAA5U/JjtsO37El8o/s1600/LightOn_Combined+Signals+Chart.jpghttp://3.bp.blogspot.com/-zuRuXBYDFoA/U6mlL1TwWNI/AAAAAAAAA48/rjfuCGMHEnA/s1600/LightOn_LowSignalLengthChart_Calibrated.jpghttp://4.bp.blogspot.com/-j8PqCR2wnao/U6mlNRQL_rI/AAAAAAAAA5M/V-QGPzH1EQs/s1600/LightOn_HighLengthFrequency.jpghttp://1.bp.blogspot.com/-L0fIETV1P-Q/U6mlMme-XMI/AAAAAAAAA5E/ar4iYl48VKk/s1600/LightOn_LowLengthFrequency.jpg
  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    8/13

    You will notice that the HIGH signals also have a repetitive pattern, however have a Very long length at theend of each section. This is almost a break to separate each section.This is what a single section looks like zoomed in:

    SL = [Short LOW] signal. - or short blue barSH = [Short HIGH] signal - or short yellow barLL = [Long LOW] signal - or long blue barLH = [Long HIGH] signal - or long yellow barVLH = [Very long HIGH} signal - or very long yellow bar (~92 analogReads in length)

    You will notice that there are only about 6 different combinations of the signals mentioned above. We canuse this to create a coding system as described below:

    We can use this coding system to describe the signals. The charts below show the difference betweenturning the LIGHT ON and LIGHT OFF.

    http://2.bp.blogspot.com/-n_0FZDRN_kk/U6mlPcENxwI/AAAAAAAAA5c/il3udhEU3RE/s1600/LightOn_Combined+Signals+Zoomed+In.jpghttp://4.bp.blogspot.com/-teEoJrwil7Y/U6mlKDPAzjI/AAAAAAAAA4s/EpBZ9B7tGiA/s1600/Coding_System.jpg
  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    9/13

    PLEASE NOTE: You may notice when you copy the signals from the Serial monitor that you get a series of(0,255) combinations. This is actually a timeout sequence - which generally occurs after the signal iscomplete.

    Here is an example of what I mean.

    This is the end of tutorial 2. In the next tutorial, we will use the code acquired from the remote to turn the FAN

    LIGHT on and off (using the 433 MHz RF transmitter).

    http://3.bp.blogspot.com/-QJFP4aNLi64/U6mlS2zl9bI/AAAAAAAAA58/fsEdIG85lkc/s1600/RF+Chart+Code+Comparison.gifhttp://1.bp.blogspot.com/-PHPdy0eSSbE/U6mlQafHi8I/AAAAAAAAA5s/1W076b4p188/s1600/TimeOut+Sequence.jpghttp://arduinobasics.blogspot.com/2014/07/433-mhz-rf-module-with-arduino-tutorial.htmlhttp://2.bp.blogspot.com/-lXz2lUaDBSk/U6mnypcD3oI/AAAAAAAAA6Q/TFd1Ct0zkUE/s1600/Code+Comparison.jpg
  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    10/13

    Posted by Scott C at 01:05

    Labels: 433Mhz,Arduino,ArduinoBasics, Capture, code, Receiver, Remote, RF

    Click here for Tutorial 3

    +13 Recommend this on Google

    Replies

    9 comments:

    caroline & Jeroen 31 July 2014 at 23:12

    Thanks for this tutorial.

    it's easy to follow, good explanation,

    i have managed to retrieve the code from a pir sensor.

    please can you give a hint or better write a tutorial how to receive the code found in tutorial 2 to starta sub in the sketch?

    thanks for this great work,

    Jeroen,

    Reply

    Scott C 1 August 2014 at 00:13

    Hi Jeroen,Can you please explain what you mean by "Start a sub in the sketch" ??If you want to know how to transmit the code you acquired in tutorial 2, then continue on totutorial 3.Please be aware that somewhere in tutorial 1&2 I have managed to get LOWs and HIGHsmixed up. Or maybe I haven't. Anyway - you will find that when you are transmitting thecode, that you have to transmit them the other way around (LOWs become HIGHs and viceversa).See how you go.

    caroline & Jeroen 1 August 2014 at 05:50

    Hi Scott, thanks for your reaction,i have also read tutorial 3 where you send the code with the Arduino.

    but that is not my question, i want to receive de code with the arduino!

    i have a pir sensor sending 433Mhz codes.as i say, i have found the code.Now i want to receive and let the arduino make a sound or something like that..

    i have no idea how to start arduino listen only to the found code ?

    i hope my question is now better.(sorry my english is not very good)

    Scott C 1 August 2014 at 08:29

    Hi Jeroen,I think I understand your question. You want the Arduino to listen for a specific code, andthen get it to do something else ONLY when it receives that code.This kind of question is always best served within an Arduino forum - where multiplerespondents can provide relevant info and code snippets etc, plus the flow of information isspecific to your question.

    Having said that - your best bet is to have a look at tutorial 4- which will show you how toget the Arduino to listen for the signal. This signal will be stored within the array called

    http://arduinobasics.blogspot.com.au/search/label/Arduinohttp://www.blogger.com/profile/10071004090415946390http://arduinobasics.blogspot.com/2014/07/433-mhz-rf-module-with-arduino-tutorial.htmlhttp://www.blogger.com/profile/10071004090415946390http://arduinobasics.blogspot.com/2014/06/433-mhz-rf-module-with-arduino-tutorial_27.html?showComment=1406823213717#c5605320200975072596http://arduinobasics.blogspot.com.au/search/label/ArduinoBasicshttp://www.blogger.com/share-post.g?blogID=4060435813840662690&postID=4931879627671540689&target=pinteresthttp://www.blogger.com/profile/13459382119854487488http://www.blogger.com/share-post.g?blogID=4060435813840662690&postID=4931879627671540689&target=bloghttp://arduinobasics.blogspot.com.au/search/label/codehttp://arduinobasics.blogspot.com/2014/06/433-mhz-rf-module-with-arduino-tutorial_27.html?showComment=1406852973910#c5502356256690359546http://arduinobasics.blogspot.com/2014/06/433-mhz-rf-module-with-arduino-tutorial_27.html?showComment=1406843414880#c3337654689636609730http://www.blogger.com/profile/13459382119854487488http://arduinobasics.blogspot.com.au/search/label/Capturehttp://arduinobasics.blogspot.com.au/search/label/Receiverhttp://www.blogger.com/share-post.g?blogID=4060435813840662690&postID=4931879627671540689&target=emailhttp://www.blogger.com/share-post.g?blogID=4060435813840662690&postID=4931879627671540689&target=facebookhttp://arduinobasics.blogspot.com.au/search/label/433Mhzhttp://arduinobasics.blogspot.com.au/search/label/Remotehttp://arduinobasics.blogspot.com.au/2014/07/433-mhz-rf-module-with-arduino-tutorial_30.htmlhttp://arduinobasics.blogspot.com.au/search/label/RFhttp://arduinobasics.blogspot.com/2014/06/433-mhz-rf-module-with-arduino-tutorial_27.html?showComment=1406819555108#c5665447783787572053http://arduinobasics.blogspot.com.au/2014/06/433-mhz-rf-module-with-arduino-tutorial_27.htmlhttp://forum.arduino.cc/http://www.blogger.com/share-post.g?blogID=4060435813840662690&postID=4931879627671540689&target=twitter
  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    11/13

    storedData[].You can then use a function to clean up the signal using "IF" statements. eg.

    if(storedData[i]

  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    12/13

    Newer Post Older PostHome

    Subscribe to: Post Comments (Atom)

    Reply

    Replies

    Reply

    Enter your comment...

    Comment as: Google Accou

    Publish

    Preview

    Feel free to leave a comment :

    Create a Link

    tj 9 October 2014 at 20:09

    Hi Scott,great tutorial thank you for the work. I'm trying to read a weather station's signal (and ultimatelydecode it), and I'll give this method a go.So far I've often seen the suggestion to hook up the receiver to your computer's Mic In and recordthe signal with Audacity, and then simply analyze the waveform visually. The audio ADC's sample rateis high enough to produce a very detailed graph.

    Anyway, I'm thinking of increasing the Arduino's analogread speed to improve the resolution. (In thehope of getting the best of both worlds.) Is your code available under an open license? I'd throw theresult on Github (assuming I produce something sensible).

    Tom

    Reply

    Scott C 9 October 2014 at 23:48

    Hi Tom,When I started this project, I did not know if it was going to work or not. When it did work, Idid a little happy dance. Not sure if it works for anyone else.

    Providing that your project is legal, I don't mind how you use my code.I would be interested to hear how your project turns out, and would be happy with just areference link back to this b log somewhere in your code or Github.

    Links to this post

    http://www.blogger.com/blog-this.ghttp://www.blogger.com/profile/10370120133969607783http://arduinobasics.blogspot.com.au/http://www.blogger.com/profile/10071004090415946390http://arduinobasics.blogspot.com/feeds/4931879627671540689/comments/defaulthttp://arduinobasics.blogspot.com.au/2014/06/433-mhz-rf-module-with-arduino-tutorial.htmlhttp://arduinobasics.blogspot.com/2014/06/433-mhz-rf-module-with-arduino-tutorial_27.html?showComment=1412856542469#c5129778174147509581http://arduinobasics.blogspot.com.au/2014/07/433-mhz-rf-module-with-arduino-tutorial.htmlhttp://arduinobasics.blogspot.com/2014/06/433-mhz-rf-module-with-arduino-tutorial_27.html?showComment=1412869697471#c2759268421283717967http://www.blogger.com/blog-this.g
  • 8/10/2019 Arduino Basics_ 433 MHz RF Module With Arduino Tutorial 2

    13/13

    2.3k

    33 MHz RF module with Arduino Tutorial 1

    HC-SR04 Ultrasonic Sensor

    Simple Arduino Serial Communication

    Bluetooth Tutorial 1

    Bluetooth Android Processing 1

    This Week's Most Popular Posts

    One in a million

    DIY Canon Intervalometer using Arduino

    Arduino Selfie

    Relay Module

    Grove Water Sensor

    Recent Posts Widget by Helplogger

    Most Recent Posts

    Awesome Inc. template. Powered by Blogger.

    http://arduinobasics.blogspot.com.au/2013/01/arduino-basics-bluetooth-tutorial.htmlhttp://www.blogger.com/http://arduinobasics.blogspot.com.au/2014/06/433-mhz-rf-module-with-arduino-tutorial.htmlhttp://arduinobasics.blogspot.com/2014/09/arduino-selfie.htmlhttp://arduinobasics.blogspot.com/2014/11/one-in-million.htmlhttp://arduinobasics.blogspot.com/2014/08/grove-water-sensor.htmlhttp://helplogger.blogspot.com/2012/04/recent-posts-widget-for-bloggerblogspot.htmlhttp://arduinobasics.blogspot.com.au/2012/07/arduino-basics-simple-arduino-serial.htmlhttp://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.htmlhttp://arduinobasics.blogspot.com/2014/10/diy-canon-intervalometer-using-arduino.htmlhttp://arduinobasics.blogspot.com/2014/09/relay-module.htmlhttp://helplogger.blogspot.com/http://arduinobasics.blogspot.com.au/2013/03/arduino-basics-bluetooth-android.html