Usding the Trinket with SPI Wireless Module nRF24L01+

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
davethewalker
 
Posts: 10
Joined: Wed Feb 12, 2014 9:09 am

Usding the Trinket with SPI Wireless Module nRF24L01+

Post by davethewalker »

Hello,

I'm wondering how best to use the Trinket to drive an nRF24L01+ wireless module. The module is SPI, but the Arduino SPI libraries don't seem to compile for the Trinket. Is there a Trinket-based SPI Library I can use, or do I need to figure out how to bit-bang the protocol? (which would seem like a shame, because there are SPI pins available on the Trinket, which is why I bought it in the first place!)

Many thanks in advance,
Dave

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by adafruit_support_rick »

SPI is built-in to the Arduino runtime for regular arduinos, but the hardware on the ATtiny is considerably different. There may be an ATtiny SPI driver out there someplace, but we don't have one.

User avatar
davethewalker
 
Posts: 10
Joined: Wed Feb 12, 2014 9:09 am

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by davethewalker »

In case anybody else hits this issue, you can just use "ShiftOut" and "ShiftIn" to talk to an SPI device, using standard IO pins. I can successfully read the status byte of my nrF24L01+ using this method. It should be a short hop from there to communicating with it properly.

JurrenP
 
Posts: 3
Joined: Thu Jun 26, 2014 12:33 pm

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by JurrenP »

DaveTheWalker wrote:In case anybody else hits this issue, you can just use "ShiftOut" and "ShiftIn" to talk to an SPI device, using standard IO pins. I can successfully read the status byte of my nrF24L01+ using this method. It should be a short hop from there to communicating with it properly.
I would like to do the same thing! Unfortunately I cannot get it to work....
Could you share some more info/code of how you did it?

User avatar
davethewalker
 
Posts: 10
Joined: Wed Feb 12, 2014 9:09 am

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by davethewalker »

Of course :o)

OK, so I have a Trinket and an nRF24L01+ module. If I connected all of the control pins to the trinket, there'd be nothing left, so I use a modified version of "Nerd Ralph"'s scheme:

http://nerdralph.blogspot.fi/2014/01/nr ... -pins.html

I say modified, because I also wanted access to the CE line, in order to be able to use some of the burst modes available on the wireless module.

Therefore:

Pin 0 = MISO
Pin 1 = free/on-board LED
Pin 2 = SCK
Pin 3 = MOSI
Pin 4 = CE

I use the 5V from the Trinket to power the VCC on the wireless module (As it has its own regulator on-board). As per Nerd Ralph, there's a red LED and a 1k resistor between 5V and CSN, and a diode between CSN and SCK (cathode to SCK). There's also a 100nF capacitor between CSN and 0V (GND).

The differences between my scheme and Ralph's is that I don't need to generate VCC from the red LED (as it has its own on-board regulator), and CE isn't pulled up to VCC, so I have the flexibility to drive it from the Trinket.

The Wireless module requires data in the following format:

<Command word: MSBit to LSBit (one byte)>
<Data Bytes: LSByte to MSByte, MSBit to LSBit within each byte>

The simplest way to demonstrate what I'm doing is by showing the following code:

Code: Select all

#define dataInPin 0
#define LEDPin 1
#define clock 2
#define dataOutPin 3
#define CE 4

#define COM_NOP 0xFF             //This is the null packet, just to get the status back

//setCE
digitalWrite(CE, HIGH);

//set CSN
digitalWrite(clock, HIGH) ;          //Set Clock HIGH, then LOW, to give a falling edge on CSN
delayMicroseconds(64);
digitalWrite(clock, LOW);
delayMicroseconds(8);

//send byte
senddata = COM_NOP;
shiftOut(dataOutPin, clock, MSBFIRST, senddata);

//reset CE
digitalWrite(CE, LOW);

//reset CSN
digitalWrite(clock, HIGH);
delayMicroseconds(64);

//setCE
digitalWrite(CE, HIGH);

//set CSN
digitalWrite(clock, HIGH) ;          //Set Clock HIGH, then LOW, to give a falling edge on CSN
delayMicroseconds(64);
digitalWrite(clock, LOW);
delayMicroseconds(8);

//receive 1 byte
byte incoming = shiftIn(dataInPin, clock, MSBFIRST);

I've actually got a lot of this set up in functions in my code, but I thought it'd be clearer to you if I just wrote it all out explicitly this time (hence the repetition).

Once I've got the data byte, I actually have a little routine that flashes the data out on the LED so I can see what was received. It does a short flash for each bit in turn, followed by a longer flash for a "1" or a long pause for a "0". It's surprisingly easy to read off the binary byte in this way, in the absence of a serial monitor.

If it worked properly, it'll read 0xE, which is 00001110 in binary.

I haven't had time to get any further in this project since I wrote the last post here, but let me know if you have any questions and we can solve them together :o)

Cheers!
Dave

User avatar
adafruit2
 
Posts: 22148
Joined: Fri Mar 11, 2005 7:36 pm

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by adafruit2 »

Oh boy, the Trinket can certainly do simple SPI like to LEDs such as WS2801 that just have a data and clock line, but it doesn't have all the pins required to do proper SPI - CS, MOSI, MISO, CLK so it may be a challenge

For stuff like this, we suggest getting it working on an Arduino but not using the hardware SPI libraries (same, bitbanging or ShiftOut) and then backporting. at least then you have 'known good' code!

JurrenP
 
Posts: 3
Joined: Thu Jun 26, 2014 12:33 pm

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by JurrenP »

Thanks a lot! I will keep you updated! :D

JurrenP
 
Posts: 3
Joined: Thu Jun 26, 2014 12:33 pm

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by JurrenP »

Ok, this morning I tried to take the first small step towards my goal.
Step 1: Get the STATUS byte using an Arduino and some code based on your example.

In your example I didn't really understand why you set CE high and low, isn't CE only used to receive or send data?
Below is the code I wrote, but the STATUS I get back is 0xFF (1111 1111) and this is also the case when I add the lines to set CE high or low.

Can you tell what this STATUS means? And why it's not 0xE?
Thanks in advance!

[update]
Ah, I forgot to define the pin modes! I have got it working now! :D

Code: Select all

#define CE 9 // CHIP ENABLE
#define CSN 10 // CHIP SELECT NOT
#define DATAIN 11
#define DATAOUT 12
#define CLOCK 13

#define COM_NOP 0xFF // Null packet to get status 

void setup()
{
  Serial.begin(9600);

  //init set CE low and CSN high
  digitalWrite(CE, LOW);
  digitalWrite(CSN, HIGH);
  
  //set CSN low to start SPI communication
  digitalWrite(CSN, LOW);
  
  //send byte
  byte senddata = COM_NOP;
  shiftOut(DATAOUT, CLOCK, MSBFIRST, senddata);
    
  //set CSN high to end SPI communication
  digitalWrite(CSN, HIGH);

  //set CSN low to start SPI communication
  digitalWrite(CSN, LOW);
  
  //receive 1 byte
  byte incoming = shiftIn(DATAIN, CLOCK, MSBFIRST);
  Serial.println(incoming, BIN);

  //set CSN high to end SPI communication
  digitalWrite(CSN, HIGH);
}

void loop()
{
}

User avatar
annihil8ted
 
Posts: 9
Joined: Mon Aug 31, 2015 4:07 pm

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by annihil8ted »

Hello!

I'm trying to copy this but I'm having trouble with the Arduino IDE. It doesn't like the serial command. Did anyone encounter this issue? Please help :)

Thanks!

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by adafruit_support_rick »

Serial doesn't exist on the Trinket. You'll have to remove that line.

User avatar
annihil8ted
 
Posts: 9
Joined: Mon Aug 31, 2015 4:07 pm

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by annihil8ted »

Was it possible once? I'm curious about why serial was in the codes above. For my application I need serial so is there a work around?

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by adafruit_support_rick »

The code JurrenP posted wasn't written for the trinket. It was written for an Arduino Uno.

User avatar
annihil8ted
 
Posts: 9
Joined: Mon Aug 31, 2015 4:07 pm

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by annihil8ted »

Thanks Admin! That makes much more sense :P I think I'll try to work with davethewalker's code instead.

User avatar
davethewalker
 
Posts: 10
Joined: Wed Feb 12, 2014 9:09 am

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by davethewalker »

annihil8ted, drop me a line if you need a hand. It has been a while since I implemented that code, but I can probably remember roughly how it worked :o)

User avatar
blckpstv
 
Posts: 55
Joined: Mon Jan 06, 2014 8:06 pm

Re: Usding the Trinket with SPI Wireless Module nRF24L01+

Post by blckpstv »

Just to be clear could I send a sensor data from this to a raspberry or a mac mini?
Because I already need 2 pins for the sensor which would have get a shortage of pins to use the nRF24L01+ , right?

I'm trying to make a very small wireless sensor project that transmits data to either raspberry or mini.

=> viewtopic.php?f=52&t=92626

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”