RGB LEDs and RF receivers.

Hello out there! Let me start with stating I'm new here, and was not sure whether to place this topic here, or in the project guidance section.

I currently have an Arduino project set up involving two Arduinos. One acts as a base station and is connected to a computer via USB. The other one is a currently an Arduino, also connected to a computer via USB for troubleshooting reasons, but is going to be a stand-alone ATMega chip.

The base station has an nRF24L01 RF tranciever wired up. It receives commands via the Serial interface and transmits them using the RF tranciever. For reference, the code used can be found here. The base station appears to be working fine. If I run a modified version of the Mirf-library ping example, the other Arduino receives the commands from the base station in a proper matter.

The second Arduino is going to be a radio controlled LED lamp. This Arduino has an RGB Power LED wired up to it, as well as a same tranciever as the base station. The code used on this Arduino can be found here. This is where the problem arises. I have written a function to convert to incoming commands (in the form #RRRGGGBBB$) to values for the red, green and blue LED, which seems to be working fine (tried it with serial input). When used over serial communication, the lamp changes and fades over accordingly. However, if I try to read the RF output, I get stuck. As you may notice in the code of the lamp, I have commented out the analogWrite functions. This is so the Arduino can receive the commands via RF properly. However, if I uncomment the analogWrite functions, the Arduino stops receiving input from the RF chip. Controlling the lamp via serial still works fine however, and the lamp changes and fades color in the right way.

Long story short: for some reason using analogWrite in my Arduino code stops my Arduino from reading from an nRF24L01 RF tranciever using the Mirf library. If I don't use analogWrite functions in my code the receiving works fine. Is there somebody out there with pointers as to how to resolve this issue?

Arduinos used are both Arduino UNOs, using the Arduino 1.0.1 IDE.

Any help would be appreciated! Thanks. :slight_smile:

for some reason using analogWrite in my Arduino code stops my Arduino from reading from an nRF24L01 RF tranciever using the Mirf library.

It is possible that the library uses the same timer as the PWM function.
To test try just enabling one analogue write at a time and see if it is only two of them that scuppers your transceiver. It will be two because two PWM pins hang off the same timer and you have three timers.

Interesting... If I try using only one analogWrite at a time, this seems to be working. However, this only goes for the blue component on pin 3. If I enable only the analogWrite for either red or green on pin 6 and 5 respectively, it still does not work. Ergo, if I try to control only the blue LED using RF, that works. This is a great start! :slight_smile:

Below is my current pin configuration. Is it possible to control 4 PWM ports in such a way that I can use them all without interfering with each other and (possibly) the millis() and delay() timer?
3~ blue RGB component
5~ green RGB component
6~ red RGB component

7 nRF24L01
8 nRF24L01
11~ nRF24L01
12 nRF24L01
13 nRF24L01

Is it possible to control 4 PWM ports in such a way that I can use them all without interfering with each other

It's not the PWM that will not work together it is the Mirf library that uses some of the resource that the PM normally uses. Your options are:-
Rewrite the Mirf library so as not to alter any timers
Use a software PWM driver like:-
http://code.google.com/p/rogue-code/wiki/SoftPWMLibraryDocumentation
However there is no guarantee that it does not mess with the same timer as the Mirf library.

After looking a bit through the Arduino Tutorials I found this tutorial on PWN: Secrets of Arduino PWM. In it it states the following.

The following code fragment sets up fast PWM on pins 3 and 11 (Timer 2).

From this I concluded that pins 3 and 11 (the latter of which is being used by Mirf) are sharing the same timer. My guess was that if I didn't use pin 3 for my LED PWM control, but 5, 6 and 10, this should not interefere with Mirf's operations. After trying this out I'm still stuck with the same problem. Do I misunderstand the concept of PWM and timers, or was this a valid assumption and should I look for the error somewhere else in my code?

I tried the SoftPWM library but also this triggered the same problem. I will try using SoftPWM on other ports later on, but it seems that the SoftPWM is also using timer 2. What I will also try is to re-write the Mirf library to use another PWM port, so timer 2 is free. Do these sound like logical steps?

My guess was that if I didn't use pin 3 for my LED PWM control, but 5, 6 and 10, this should not interefere with Mirf's operations.

That doesn't coincide with what you said earlier:-

If I try using only one analogWrite at a time, this seems to be working. However, this only goes for the blue component on pin 3.

This suggests it is not timer 2 that is being used by the library.
Pins 5 and 6 use timer 0
Pins 9 and 10 use timer 1
Pins 3 and 11 use timer 2

Anyway it is not the use of any pin by the library but what it does with the timers that is important to you. Look at the libary code and see what it does.

I was able to fix the problem with the help of an electrical engineering student in my faculty.

The solution was to use an Arduino Leonardo, instead of an Arduino Uno, because the Uno overlaps some ports used for the Spi interface (which Mirf uses) with the PWM ports. The Arduino Leonardo has these ports sperated, allowing for both the RF transciever and the PWM ports to work properly. :slight_smile: