Hot!RN4020 BLE module - getting started

Author
Sigismund91
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2017/06/06 22:23:22
  • Location: 0
  • Status: offline
2017/06/09 02:09:40 (permalink)
0

RN4020 BLE module - getting started

Hi!
 
I've been working on an Android app. I've bought a RN4020 module to communicate with my android device. RN4020 communicates with PIC16F877A MCU on the board over UART (at least it should). The problem is, I've tried so many things to "revive" RN4020, but I did not succeed. My Android device can't discover the module. I'm using Microchips RN4020 DEMO app and SmartDiscover app. I only have the module, not the whole PICTAIL kit, so demo videos with terminal and stuff doesn't help me a lot. 
 
This is the following schematics:
The module: 
 
PIC: 
 
So, the minimum connections I have with RN4020 are: UART TX, UART RX, WAKE_HW and WAKE_SW.
On the RN4020 I've subsequently connencted the CMD/MLDP pin to the PIC pin 2 (RA0) and connected the RTS pin to the PIC pin 6 (RA4), both over the wire. Doing so and trying the "flow control" and CMD/MLDP suggestions based from read on this forum, still no reaction from the module. I do get the "ACTIVE" pin high and the blue LED lights ON.  
 
The only thing I want to do right now is to be able to discover the module with my android device, which supports the 4.0 BT. 
UART is set as calculated with the Microchip app (see photo:
Since PIC is on 5V supply and RN4020 is on 3V3 i've even put the 10k ohm pull-up resistor on PIC Rx side, but still no effect. 
 
So, my question is, what did I miss? This is the following code I assume should work (but apparently isn't :) ):
 
THE UART:

#define UART_TX_Empty() TXSTAbits.TRMT
 
char UART_Init(const long int baudrate)
{
   BRGH = 1; //High Baud Rate select bit
   SPBRG = 9; //Writing SPBRG Register
   SYNC = 0; //Setting Asynchronous Mode, ie UART
   SPEN = 1; //Enables Serial Port
   TRISC7 = 1; //As Prescribed in Datasheet
   TRISC6 = 1; //As Prescribed in Datasheet
   CREN = 1; //Enables Continuous Reception
   TX9 = 0; //8-bit transmition 
   RX9 = 0; //8-bit reception
   TXEN = 1; //Enables Transmission
   return 1; //Returns 1 to indicate Successful Completion
 }
 
 
void UART_Write(char data)
   {
   while(!TRMT); //Waiting for Previous Data to Transmit completly
   TXREG= data; //Writing data to Transmit Register, Starts transmission
   }
 
 
void UART_Send_String(char* string)
   {
   char* text = string;
   while(*text != '\r')
     {
        if(UART_TX_Empty())
        UART_Write(*text++);
     }
     UART_Write('\r');
   }
 
char UART_Data_Ready()
   {
   return RCIF;
   }
 
char UART_Read()
   {
     while(!RCIF);
     return RCREG;
   }
 
 
void UART_Read_Text(char *Output, unsigned int length)
     {
        unsigned int i;
        for(int i=0;i<length;i++)
        Output[i] = UART_Read();
     }

 
 
Some of the code might bit strange, but I've changed it A LOT recently, so some parts are the "sins of the past" ;)

Here is the PIC code:

#include "Fabregas.h"
#include "lcd_driver.h"
#include "uart.h"
#include <xc.h>
#include <stdio.h>
 
unsigned char i = 0;
unsigned char received_temp;
unsigned char received_temporary; 
unsigned char buzzer;
unsigned char fan;
unsigned char blue;
unsigned char green;
unsigned char red;
unsigned char test;
 
const char factory_reset[] = {'S','F',',','1','\r'}; //Reset to factory default configuration
const char peripheral[] = {'S','R',',','3','2','0','0','0','0','0','0','\r'}; //act as a peripheral with automatic advertisement
const char name[] = {'S','-',',','F','a','b','r','e','g','a','s','\r'}; //set device name
const char reboot[] = {'R',',','1','\r'}; //Reboot RN4020 to make the changes effective.
const char baud_rate[] = {'S','B',',','1','\r'};
const char info_bat[] = {'S','S',',','3','0','0','0','0','0','0','0','\r'}; //enable device informaction and battery
const char advertising[] = {'A','\r'}; //start advetising
 
#define a PORTCbits.RC3
#define b PORTCbits.RC2
#define c PORTCbits.RC1
#define d PORTCbits.RC0
#define PORTBbits.RB0
#define PORTCbits.RC4
#define PORTCbits.RC5
#define buzzer PORTAbits.RA3
#define fan PORTBbits.RB5 
#define blue PORTAbits.RA5 
#define green PORTBbits.RB1 
#define red PORTBbits.RB3
#define SW_wake PORTAbits.RA2
#define HW_wake PORTAbits.RA1
#define CMD_MLDP PORTAbits.RA0
#define RTS PORTAbits.RA4
 
void main()
{
TRISB = 0x00;
TRISC = 0x80; 
TRISD = 0x00; 

Lcd8_Init();
UART_Init(115200); 

ADCON1 = 0x0E; 
TRISA = 0x00; 
TRISAbits.TRISA4 = 1; 


buzzer = 0; 
fan = 0;
a=1;
b=1;
c=1;
d=1;
e=1;
f=1;
g=0;
blue = 0; 
green = 0; 
red = 0;
 
 
Lcd8_Clear();
SW_wake = 1;
HW_wake = 1;
CMD_MLDP = 0;
 
UART_Send_String((char*)factory_reset);
UART_Send_String((char*)peripheral);
UART_Send_String((char*)name);
UART_Send_String((char*)reboot);
 
__delay_ms(100); //give RN4020 some time
__delay_ms(100); //give RN4020 some time
 
UART_Send_String((char*)advertising);
 
Lcd8_Set_Cursor(1,0); 
Lcd8_Write_String("OK!");
    while(1)
   {
   }
 
}

 
 So, can someone please help m? This is what I get from the circuit at the end:

Attached Image(s)

#1
PIC4C code
Starting Member
  • Total Posts : 62
  • Reward points : 0
  • Joined: 2011/02/21 10:32:55
  • Location: 0
  • Status: offline
Re: RN4020 BLE module - getting started 2017/06/10 06:06:47 (permalink)
5 (1)
The RN4020 has been around for a long time and a good choice.  I don't know what you mean by "at least it should".  Connect to you virtual terminal (something like TeraTerm) and start your debugging e.g.  terminal set-up, baud rate, data bits, parity, stop bit then go to your command sets and check what is going on.  Also you might want to set device to factory reset.  Remember that most of you device's UARTs have a lot of build in presets.
Good luck. 
#2
RISC
Super Member
  • Total Posts : 4022
  • Reward points : 0
  • Status: online
Re: RN4020 BLE module - getting started 2017/06/10 08:50:05 (permalink)
3.5 (2)
Hi,
If your PIC16F877A has a 5V Vdd and RN4020 has a 3.3V Vdd you will have severe issues...unless you use logic level translators.
But the easiest way is to use PIC16LF877A at 3.3V at 10MHz or use another more recent PIC16 if you need to run at a higher frequency like PIC16F18877
Regards
#3
piccandies
Super Member
  • Total Posts : 221
  • Reward points : 0
  • Joined: 2008/08/12 23:12:23
  • Location: 0
  • Status: offline
Re: RN4020 BLE module - getting started 2017/06/11 13:20:30 (permalink)
0
For the moment, forget about the RN4020...get a serial to usb adapter (ebay, $1) & ensure that your pic is spitting out the proper chars at the proper baud (115.2K baud)...Would eliminate one extra chance of trouble if you run both chips at 3.3V...At very least put 1k in series between the pic TX & 4020 RX..to "prevent" overdrive, and keep you RX pullup. Tie your cmd line low, make both wake lines hi.  For now (and probably forever), just tie RTS to CTS, not the pic.
Send:
SF,1
SR,32000000
R,1 
This will be enough to find it on your Android  !!!
It is barely mentioned anywhere that GR ,GT, etc will Get & spit out the settings from the 4020 Uart, so you can see they were set to your likings (S=Set, G=Get) on the terminal.
V will spit out the version of the RN4020 .
YOU can actually just hook up the serial adapter from the PC terminal to the RN4020 (no PIC used at all), just to get the practice for using the RN4020 commands, so then you know what to do.  This is the best way to check your RN4020 is working.
 
Note that the Android should be unpaired from the 4020 & maybe powered down & up so that any later new changes are detected (such as changing the names) by the phone.  Unpairing is slightly hidden in the settings...
 
Paring with the 32000000 config will ask for a random password which will be spit out of the RN4020 onto your terminal.  If you don't have a terminal (to see it), and just enter some junk...it will pair but won't work & won't tell you there is a problem.  Even if you change things, the phone will remember the wrong password & never cooperate (and not tell you there is an issue), until you unpair & re-pair.  SR32060000 will not ask for any password & will pair without any issue (but must unpair first if it was mispaired)
#4
Sigismund91
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2017/06/06 22:23:22
  • Location: 0
  • Status: offline
Re: RN4020 BLE module - getting started 2017/06/11 22:05:29 (permalink)
0
piccandies thank you so much for the reply! I will try what you wrote and come back with the results! I hope it'll work =)
#5
piccandies
Super Member
  • Total Posts : 221
  • Reward points : 0
  • Joined: 2008/08/12 23:12:23
  • Location: 0
  • Status: offline
Re: RN4020 BLE module - getting started 2017/07/15 21:51:46 (permalink)
4 (2)
 I will try what you wrote and come back with the results!
By now you must have the results & failed to report them...this results in a $0.50 fine/day
#6
joab
New Member
  • Total Posts : 29
  • Reward points : 0
  • Joined: 2017/05/25 05:55:05
  • Location: 0
  • Status: offline
Re: RN4020 BLE module - getting started 2017/07/17 00:19:30 (permalink)
0
First, you have the PIC powered at 5V and the BL at 3V3, you need either voltage translators or work at the same voltage on both.

But this doesn't seem to be your immediate problem, as soon as the module is powered up it starts. If you can't find it, maybe it's not advertising, did you configure it before for not advertising? Those settings remain in flash. You could try a factory-reset, it doesn't need communications.
Try the divide-and-conquer philosophy, get a USB-UART cable and check your comms with both the PIC and BL module, to see where the problem is. I experienced some weird things with RN4020, sometimes they didn't respond at all, I assumed they were dead but some time after (unplugged) they worked fine again...
#7
Sigismund91
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2017/06/06 22:23:22
  • Location: 0
  • Status: offline
Re: RN4020 BLE module - getting started 2017/07/19 02:40:53 (permalink)
0
@piccandies, you are right! In my defence, there was a change in the design, so i've been waiting for the new PCB. Plus, the new selected PIC16LF877a which works on 3.3V as well was not availible in Farnell last time we ordered. Don't worry, I'll talk to you AS SOON as I get the results, just as I promised. Unfortunatelly, this is not a priority project here at work, so it still might take a while + on friday we take 14 day off....
 
@joab thank you for the advice! I've tested the UART communication with PIC over HTerm and it worked fine..did not manage to test the BLE yet...super busy at work 
#8
Jump to:
© 2017 APG vNext Commercial Version 4.5