MOSFET Mayhem..

Hey Forum,

This post is in reference to my last post about mapping analog data to shift registers.

To summarize, I’m attempting to use an N-channel MOSFET transistor to drive a series of LEDs and their brightness (PWM) based on analog data from an accelerometer. I’ve succeeded in mapping the analog data to LEDs, but by using a pin off the Arduino for each LED so now, I want to consolidate everything and use the transistor to PWM all the LEDs only using one pin off the Arduino.

However, I’m stuck - I can’t turn on the LEDs using the transistor. This is the original code I used to PWM individual pins:

const int Xdata = A0;  // Analog input pin that the accelerometer is attached to
const int greenPin1 = 2; // Analog output pin that the LED is attached to
const int greenPin2 = 3;

int sensorValue = 0;        // value read from the accelerometer
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(Xdata);            
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 525, 0, 654);  
  // change the analog out value:
  analogWrite(greenPin1, outputValue);
  analogWrite(greenPin2, outputValue);

  // print the results to the serial monitor:
  Serial.print("sensor = " );                      
  Serial.println(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);  

  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(50);                    
}

I’m having trouble combining this sketch with the last example sketch proposed by Nick Gammon to achieve the overall LED pwm effect. It’s slightly confusing because I’m no longer using the shift register like the sketch implies:

#include <SPI.h>

const byte LATCH = 10;

void setup ()
{
  SPI.begin ();
}  // end of setup

const byte maxDimIterations = 63;

byte c;
void loop ()
{
  c++;
  
  for (byte bright = 0; bright < maxDimIterations; bright++)
    {
    analogWrite (5, bright * 4);
    digitalWrite (LATCH, LOW);
    SPI.transfer (c);
    digitalWrite (LATCH, HIGH);
    delay (10);
    }  // end of for
    
}  // end of loop

NOT TO MENTION the example sketch on bildr.org for transistors doesn’t really help clarify things…:

//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code to output a PWM sine wave signal on pin 9
//////////////////////////////////////////////////////////////////

#define fadePin 3

void setup(){
  pinMode(fadePin, OUTPUT);
}

void loop(){

  for(int i = 0; i<360; i++){
    //convert 0-360 angle to radian (needed for sin function)
    float rad = DEG_TO_RAD * i;

    //calculate sin of angle as number between 0 and 255
    int sinOut = constrain((sin(rad) * 128) + 128, 0, 255); 

    analogWrite(fadePin, sinOut);

    delay(15);
  }

}

Any insight you can give is greatly appreciated. Thanks!

That last sketch should work, so how have you wired it up and what sort of FET are you using? A schematic would be a good thing to post.

That last sketch should work, so how have you wired it up and what sort of FET are you using?

I’m using an 60V 30A N-channel MOSFET like this one:

Basically I am trying to replicate this schematic (by Nick Gammon) but I would like to leave out the shift registers and power the LEDs directly by the MOSFET.

(I’m still very new to eagleCAD so any schematic I draw will probably confuse you more.)
The issues I’m having are:

  1. The correct wiring set up (where the schematic comes in handy)
  2. Combing the previous sketches to map analog data to the FET to drive this configuration.

Any input helps! Thanks.

That circuit gives you individual control over each LED as well as a master brightness using the FET.

If you want to get rid of the SR then just connect the resistors to 4 Arduino digital outputs.


Rob

This is electronics, words such as a FET LIKE this one do not cut it. We need to know exactly what FET you are using, is it that one? The same goes for the schematic we need to know exactly what you have wired up not what it is similar to.

The thing is you have made an error somewhere and we are trying to track it down so if all you provide is the good stuff you are working from we can't tell what you have done. Everything so far should work but as it dosn't it means you have deviated in some way that both you and us don't know.

This is electronics, words such as a FET LIKE this one do not cut it. We need to know exactly what FET you are using, is it that one?

Well Grumpy Mike there's actually a link to the product page for the MOSFET I am using in my last post but I'll post it again: http://www.sparkfun.com/products/10213

I'm new to this so I'm not very astute in the language but according to the data sheet I am using a 30A, 60V, ESD Rated, 0.047 Ohm, Logic Level N-Channel Power MOSFET. Part Number: RFP30N06LE Brand: P30N06LE

I hope that is sufficient information to identify the part I'm using...

The same goes for the schematic we need to know exactly what you have wired up not what it is similar to.

Again, I am new to this so using CAD programs to create schematics is something I am still learning. The last schematic I posted (Courtesy of Nick Gammon) is the most recent iteration of an ongoing designing process so I am attempting to revise that schematic and its corresponding sketch to achieve the same effect without the shift registers. Not posting it would've given less information to understand what I am describing which, I'm assuming, would make you even more grumpy, Mike.

The same goes for the schematic we need to know exactly what you have wired up not what it is similar to.

As for my question, I am specifically asking for assistance in writing a script that receives analog input and maps that data to a PWM pin which will drive the aforementioned MOSFET to control the brightness of a series of LED lights. I have shown EXAMPLES of what I am talking about to give you a better understanding of what I am trying to achieve because no example is EXACTLY what I am trying to accomplish so I must work to incorporate details of each example.

Thanks for any input!

Insert Quote That circuit gives you individual control over each LED as well as a master brightness using the FET.

If you want to get rid of the SR then just connect the resistors to 4 Arduino digital outputs.

Hey Rob! In that situation, I would still need to use 4 pins off the Arduino though. My goal is to use one pin to control the PWM of the MOSFET and use that to drive all the LEDs.

there's actually a link to the product page for the MOSFET

Good but you didn't say that you said:-

I'm using an 60V 30A N-channel MOSFET like this one:

The "LIKE this one" to me means "something similar to" not "this identical one". Mind you I have only been speaking this language 60 years so what do I know. Probably more than you anyway. Anyway yes that FET is suitable for what you are trying to do with it, so that rules out an error in component choice.

Not posting it would've given less information to understand what I am describing

No not posting it would give me no clue as to what you are trying to do, posting what you did gives me only half a clue as to what you are doing. If you are not capable of describing what you are actually doing what makes you think any one can spot what you are doing wrong.

As for my question, I am specifically asking for assistance in writing a script that receives analog input and maps that data to a PWM pin which will drive the aforementioned MOSFET to control the brightness of a series of LED lights.

Unless you get the hardware right then you have no chance in doing anything fancy with it. The first thing you must do is to write something that just writes values to the analogue write and see that the LEDs respond. Without that you are wasting your time trying to do anything else.

With that schematic you posted I know how I would change that to do what you want, but the question is do you know how to change it to do what you want. By asking you what you actually wired up I was trying to assess if you did, so that I could put you right if you didn't.

If you don't want help then why ask in the first place.

My goal is to use one pin to control the PWM of the MOSFET and use that to drive all the LEDs.

So you don't need individual control over each LED as well?

In that case just wire the resistors to +v.


Rob

Thanks Rob!
My question now, though, is how to combine the two following sketches to instruct the circuit to work the way you and I are describing. The second sketch is an example from the bildr.org site and I am showing you because I have never used a MOSFET component before and this is the best way I can describe to you my intentions…

SKETCH1:

const int Xdata = A0;
const int greenPin1 = 2;
const int greenPin2 = 3;
const int greenPin3 = 4;
const int greenPin4 = 5;
const int greenPin5 = 6;
const int greenPin6 = 7;
const int greenPin7 = 8;
const int greenPin8 = 9;
const int greenPin9 = 10;

int sensorValue = 0;
int outputValue = 0;

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

void loop() {
 
 sensorValue = analogRead(Xdata);            
  outputValue = map(sensorValue, 0, 525, 0, 654);  

  analogWrite(greenPin1, outputValue);
  analogWrite(greenPin2, outputValue);
  analogWrite(greenPin3, outputValue);
  analogWrite(greenPin4, outputValue);
  analogWrite(greenPin5, outputValue);
  analogWrite(greenPin6, outputValue);
  analogWrite(greenPin7, outputValue);
  analogWrite(greenPin8, outputValue);
  analogWrite(greenPin9, outputValue);

  Serial.print("sensor = " );                      
  Serial.println(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);  

  delay(50);                    
}

SKETCH2:

#define fadePin 3

void setup(){
  pinMode(fadePin, OUTPUT);
}

void loop(){

  for(int i = 0; i<360; i++){
    //convert 0-360 angle to radian (needed for sin function)
    float rad = DEG_TO_RAD * i;

    //calculate sin of angle as number between 0 and 255
    int sinOut = constrain((sin(rad) * 128) + 128, 0, 255); 

    analogWrite(fadePin, sinOut);

    delay(15);
  }

}

Thanks for your input!

In the case of sketch 2

analogWrite(fadePin, sinOut);

creates a square wave on fadePin. That pin is connected through a small resistor (~150R) to the FET gate. The drain goes to the LED cathodes, source to GND. LED anodes to resistors and resistors to +V.

Also the FET should be a "logic level" FET that will turn on properly with just 5v on the gate.


Rob