Pwm from npn seen on anlog input

Hey all i am working on a small project where i have three switched that are led lit according to which button is pushed. then i am trying to read a potentiometer using the analog input. what i have found by using the graph in arduino ide is that the pwm is directly influenceing the reading.

When my code first starts up it pulses all three then based on which button that is pushed it only pulses that button.

Using averaging i can get it down to a variance of 3 to 6. the problem is that i only want to output a variable when the pot is moved. Currently using a Hysteresis out put to only out put a variable update if it is above or below 4. It is working ok.

So far i have tried capacitors in a couple spots which helped. Am i expecting to much and should be happy with what i have or is there something i am missing that could improve the performance.

Thanks!

This thread is useless without schematics.

It would help to see a circuit diagram and particularly your code (in code tags please).

Your subject talks about "npn" but the main description never mentions anything like that. You talk about PWM but where, what pins, controlling what? And a variance of 3 to 6 what? What is the "variable" and where do you want to output it to ?

I'd like to help but I'm sorry, I have no real idea what you're talking about.

Steve

sorry guys, posted from my phone, as soon as i get home i will upload a schematic.

I would love to post my code... only concern is i have been yelled at, OK scolded, actually just plain told i would get no support because of what i am trying to accomplish... So if you can over look the name of certain variables i can post as is, or else i can modify it to be more... generic?

Oh are you doing something nefarious, illegal, or dangerous? If so then you should work on your own. It really isn't fair to implicate us.

I looked through your old posts. I don't see it.

No nothing like that, i would say it is none of those.

I am build a control unit to drive a alternator as a motor for a small boat.

It was under my question about processing power of a mega. Though i will be honest i wasn’t much offended by it, but over on the mbed forum was much worse… i just don’t want to be getting my self in trouble.

Oh I see the thread. Yeah, it's on the dangerous end. Seriously, if you want to do something like that then pick something more reliable than an Arduino. Think about what happens when it fails. You lose control of a boat. And get someone pro to help design it.

You do understand why folks wouldn't want to get involved in that right? It's not that it's a terrible idea on its own, but being designed on a hobby electronics board by someone asking questions on an Internet forum makes it a pretty sketchy deal.

It's like showing up on some health forum and asking a bunch of questions about where the spleen is because you're thinking about trying to do your own surgery. The fact that you're asking shows the lack of qualification. You don't have the right tools. And someone could get seriously hurt.

I guess dangerous is a matter of opinion, some people jump out of planes for fun. The safety protocols i plan on having in place will allow the system to operate with a faulty module or will shut everything down in the event of a malfunction in the motor driver device. Also the reason why there is prototyping, small and moving to something large when proved viable.

Hey as for Arduino being reliable, i have a kureig water filler setup that has been running for almost 2 year never hiccuped once...

Oh and btw this is something different though still dealing with boats lol.

tallguy64:
I guess dangerous is a matter of opinion, some people jump out of planes for fun.

Yes, and when they do so as amateurs they always do so under the supervision of an experienced instructor. Anyone who just goes and jumps out of a plane without the experience or knowledge of how to work a parachute is just stupid.

tallguy64:
The safety protocols i plan on having in place will allow the system to operate with a faulty module or will shut everything down in the event of a malfunction in the motor driver device.

You claim to have all these safety protocols and talk like you are some expert, but then you ask questions that no expert would have to ask. See the confusion?

tallguy64:
Also the reason why there is prototyping, small and moving to something large when proved viable.

OK, so start with a model boat. Then this isn't such a dangerous thing. Nobody can get hurt during the testing and prototyping phase there.

tallguy64:
Hey as for Arduino being reliable, i have a kureig water filler setup that has been running for almost 2 year never hiccuped once...

Oh, I didn't know that. So I guess that means that no Arduino will ever fail anywhere ever. That's aweome. Hey everybody, Arduino is OK in safety critical devices now because this guys water filler worked for almost two years. And I guess anyone who has ever seen a failure was imagining things. Surely you're smarter than that. If not then you should probably leave the boat the way it came.

You are right i am not an expert in hardware development or software, but I get safety. I do safety everyday when i am building off-road ad-dons for vehicles.

This is why i am here asking questions, yes i do get it is still the internet and everything should still be taken with a bit of common sense. (i know that is a whole argument in its self)

I get comparing between a motor control system and a water filling system is not the same thing. Everything will break, and if you plan for this then you are a step ahead. Going knowing it will fail and plan for it when developing safety protocols.

tallguy64:
I get comparing between a motor control system and a water filling system is not the same thing.

That’s not the point. The point is that just because one thing didn’t fail doesn’t mean that the system is failure proof. And you’re talking about a very different environment for it on a boat.

First of all i never said it was failure proof, all i meant was that i feel it is not an unstable platform to work with.

Have you ever had a throttle cable stick on a car causing an engine to go full throttle when it was running, had to fix one on a customers vehicle.

Have you ever had a clutch fail, not being able to disengage the engine from the transmission, had to fix one.

Have you ever had a drive line snap in half on a jeep, had to fix that plus the aftermath of the drive-line spinning and braking both rear brake lines.

Have you ever had an ECM fail on a car leaving you stranded on the side of the road.

Have you ever snapped a pit man arm on your truck, leaving you with no steering not being able to control your truck. (this happened to me personal last week, and got luck it broke at a stop light)

I am just trying to point out everything will fail no matter how well it is planned for.

If it would help i could out line the safety protocols that i plan on implementing first before i receive any help?

Maybe you should do a little more maintenance, or get better vehicles. None of what you stated started off as inadequate.

tinman13kup:
Maybe you should do a little more maintenance, or get better vehicles. None of what you stated started off as inadequate.

Tinman you are very right. All of those examples were caused by either lack of maintenance or modification and used in a way not how they were originally designed, all by customers except the pit-man arm which was mine. still trying to figure out how i broke a 3inch cast piece of steel :o .

But like i said all of them were fixed, the best example was the jeep where adding a skid plate and a drive line hoop he hasn't had any problems since and if he does break another drive-line bashing off rocks it wont take out his brake lines this time.

Safety.

tallguy64:
and used in a way not how they were originally designed,

Like using a hobby grade board to control a motor boat? Am I following this?

Now we could argue forever, but I see that you're pretty stuck on using a toy to mod your boat. OK, please don't drive it near me is all I ask.

If you want to get to the root of your PWM analogRead problem, then write a simple no-frills example sketch that illustrates the problem and some basic hardware to recreate it and we can have a look at that I think as long as it is isolated from this whole control a boat with a hobby prototyping board nonsense.

But tell me one thing first. If I came to your shop and said I wanted to put the wheels and tires off my kids tonka truck on my 4x4 because I though they'd look cool and wanted you to build adapters for them, would you help me out with that or go to no small effort to try to make me realize how stupid that would be? If I told you to back off that I was an expert in wheels and tires and I knew this was an OK thing to do, would that change your mind about me? Would you help me or still tell me how stupid it was?

What if I said I wanted to replace my drive-shaft with a wooden one to save weight. What if I told you that my daddy's house has an oak beam in it that has been standing for 120 years, so I know it's strong enough. Would you help me whittle one and send me out on the road with it? Or would you try your best to convince me how stupid of an idea that was?

Well that's what we have here with you basically saying you want to make your boat drive by wire using a hobby micro-controller. Real drive by wire systems are orders of magnitude more robust in design than that, and even then they're not used in very many cases because of the very reasons we are talking about. You find them in airplanes that cost millions of dollars sure. But you don't see any passenger cars out there running drive by wire for anything aside from throttle control because even the big boys at the automakers can't get them good enough t pass NTSA. Safety protocols or not, doing dangerous things is still dangerous. It is impossible for you to anticipate any possible failure so it is impossible to say that you've accounted for them all.

If you want to get into drive by wire design then by all means do it. But don't rely on Arduino for it.

Delta_G:
But you don't see any passenger cars out there running drive by wire for anything aside from throttle control because even the big boys at the automakers can't get them good enough t pass NTSA. Safety protocols or not, doing dangerous things is still dangerous. It is impossible for you to anticipate any possible failure so it is impossible to say that you've accounted for them all.

Drive by wire car

I would like to mention mega squirt, should look that up started as a 8bit chip, the latest development was in 2010 using a MC9S12XEP100 and that is used to run engines that could over fuel and create a fiery death....

I feel that we have gotten a bit off topic… i am sorry. i attached a pdf of my current schematic sorry it is kind hard to read slapped it together really quick. so you can see what i am working with.

const int fwButton = A2;
const int ntButton = A3;
const int rvButton = A4;

const int fwLed = 2;
const int ntLed = 3;
const int rvLed = 4;

const int bz = 8;

int brightness = 0;
int fadeAmt = 20;
const long fadeInt = 100;

int fwState = 0;
int ntState = 0;
int rvState = 0;

int shiftState = 0;

//THROTTLE CALL OUTS

const int numReadings = 20;

int readThrottle1[numReadings];
//int readThrottle2[numReadings;

int throttle1Index = 0;
//int throttle2Index = 0;

int throttle1Total = 0;
//int throttle2Total = 0;

int throttle1Average = 0;
//int throttle2Average = 0;

const int throttle1Source = A0;
//const int throttle2Source = A1;

int throttle1 = 0;
//int throttle2 = 0;

int Oldthrottle1 = 0;
//int Oldthrottle2 = 0;



unsigned long previousMillis = 0;
unsigned long currentMillis = 0;

void setup()  {

  Serial.begin(9600);

  pinMode(bz, OUTPUT);

  pinMode(fwButton, INPUT);
  //pinMode(ntButton, INPUT);
  pinMode(rvButton, INPUT);

  pinMode(fwLed, OUTPUT);
  //pinMode(ntLed, OUTPUT);
  pinMode(rvLed, OUTPUT);

  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readThrottle1[thisReading] = 0;
  }


  beep(50);
  beep(50);
  beep(50);

}

void loop()  {

  fwState = digitalRead(fwButton);
  ntState = digitalRead(ntButton);
  rvState = digitalRead(rvButton);

  // throttle1 = analogRead(throttle1Source);
  //  throttle2 = analogRead(throttle2Source);

  if (fwState == 1 && rvState == 0 && ntState == 0) {
    shiftState = 1;
    analogWrite(bz, 100);
  }

  if (fwState == 0 && rvState == 0 && ntState == 1) {
    shiftState = 2;
    analogWrite(bz, 100);
  }

  if (fwState == 0 && rvState == 1 && ntState == 0) {
    shiftState = 3;
    analogWrite(bz, 100);
  }

  if (fwState == 0 && rvState == 0 && ntState == 0) {
    analogWrite(bz, 0);
  }



  // subtract the last reading:
  throttle1Total = throttle1Total - readThrottle1[throttle1Index];
  // read from the sensor:
  readThrottle1[throttle1Index] = analogRead(throttle1Source);
  // add the reading to the total:
  throttle1Total = throttle1Total + readThrottle1[throttle1Index];
  // advance to the next position in the array:
  throttle1Index = throttle1Index + 1;

  // if we're at the end of the array...
  if (throttle1Index >= numReadings) {
    // ...wrap around to the beginning:
    throttle1Index = 0;
  }

  // calculate the average:
  throttle1Average = throttle1Total / numReadings;
  // send it to the computer as ASCII digits
  //Serial.println(throttle1Average);

  if (throttle1Average > Oldthrottle1 + 4 || throttle1Average < Oldthrottle1 - 4 ) {
    Serial.println(throttle1Average);
    Oldthrottle1 = throttle1Average;
  }


  fade();

}


void beep(unsigned char delayms) {
  analogWrite(bz, 100);      // Almost any value can be used except 0 and 255
  delay(delayms);          // wait for a delayms ms
  analogWrite(bz, 0);       // 0 turns it off
  delay(delayms);          // wait for a delayms ms
}

void fade() {

  currentMillis = millis();

  if (currentMillis - previousMillis >= fadeInt) {
    previousMillis = currentMillis;

    brightness = brightness + fadeAmt;

    if (brightness <= 0 || brightness >= 255) {
      fadeAmt = -fadeAmt;
    }

    if (shiftState == 0) {
      analogWrite(fwLed, brightness);
      analogWrite(ntLed, brightness);
      analogWrite(rvLed, brightness);
    }

    if (shiftState == 1) {
      analogWrite(fwLed, brightness);
      analogWrite(ntLed, 25);
      analogWrite(rvLed, 25);
    }

    if (shiftState == 2) {
      analogWrite(ntLed, brightness);
      analogWrite(fwLed, 25);
      analogWrite(rvLed, 25);
    }

    if (shiftState == 3) {
      analogWrite(rvLed, brightness);
      analogWrite(ntLed, 25);
      analogWrite(fwLed, 25);
    }

  }
}

throttle.ino (3.82 KB)

throttle board.PDF (216 KB)

So i hooked up my scope to the circuit, i am finding about a 31.4 to 90ish mV noise on the analog input to the arduino.

I am wondering if the problem is more power supply related, i am not seeing the pattern i thought i saw related to the pwm. Any ideas how to clean that up any more?

Also something else that i noticed was the sizeable spike coming from the due pwm output once and a while. should i be concerned? i am but not sure if i should be.

Thanks