PWM signal corrupted on pin 6 but not pin 5

Hi there. i have an led on pin 5 and 6. when the complexity of my program increases to a certain point, pin 6 LED starts flickering. i can see its signal is corrupted with my scope. as they both run from the same timer i am confused how this is happening. i am refraining from going too deep into my circuit or code just for the moment as both are quite complex.
In a nutshell, can a library conflict affect a single pin instead of all the pins which use that timer?
if anyone has any suggestions for what could be causing this i would be very grateful. thanks.

I'm going to refrain from posting an answer just for the moment.

the guys got wit.

Every access to the pin or related timer registers can affect the output signal. E.g. if the duty cycle is frequently changed...

Whenever i have experienced conflicts in the past like when using the servo library for example it makes all of the pins which use the same timer as the library act strangely. is it possible for a timer conflict to affect only a single pin or is it likely another problem? thanks.

gonadgranny:
Whenever i have experienced conflicts in the past like when using the servo library for example it makes all of the pins which use the same timer as the library act strangely.

Based on your own experience you are facing an anomalous situation yet you won't post the program that is exhibiting the problem?

...R

i am refraining from going too deep into my circuit or code just for the moment as both are quite complex.

Excellent idea! No need to bother anyone with the actual details.

I'd investigate the issue by un-complexifying the code and circuit in stages until the problem goes away, then add a bit back to verify that's the cause.

In a nutshell, can a library conflict affect a single pin instead of all the pins which use that timer?

Yes

Ok, i’ve simplified my code and found that when it is running as i have posted then then led2 flickers like mad. however i can stop it from flickering by commenting out:

Serial.begin(9600);

or;

charge_state = digitalRead(charge_pin);

or;

strip.show();

or;

analogWrite(led1_pin,100);

so there seems to be some funny interaction between these processes. could anybody please help elucidate what might be happening now that i have posted my code? Cheers.

//neopixel library
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif


/////******PINS*****///////
//output pins
#define bc1_pin A1 //switch high to turn on the BC powering arduino, mp3 module, amp and neopixels
#define bc2_pin A0 //switch high to turn on servo BC
#define sink_pin A4 //switch low to sink current for voltage dividers and high to stop sinking(save current)
#define mos1_pin 10 // the audio Mosfet
#define mos2_pin 11 //servo mosfet 
#define neo_pin 12
#define mute_pin 13


#define led1_pin 5
#define led2_pin 6
#define ir_pin 9
#define ldr_pin A2
#define battery_pin A5
#define charge_pin A6
#define bagSwitch_pin A7


//////*******VAIABLES*******////////
bool handSwitch_state = false;
long ir_val = 0;
int ldr_val;
int battery_val;
bool charge_state;
int bagSwitch_val;

int battery_voltage;
int led2_val;

//PIN TOGGLES
bool bc1_tog = true;
bool bc2_tog = false;
bool mos1_tog = true;
bool mos2_tog = false;
bool led2_tog = false;
bool led1_tog = false;



///****OBJECT INSTANTATIONS****////
Adafruit_NeoPixel strip = Adafruit_NeoPixel(3, neo_pin, NEO_GRB + NEO_KHZ800);//create a neopixel object

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

  pinMode(bc1_pin, OUTPUT);  digitalWrite(bc1_pin, HIGH); //switch on the main boost converter
  pinMode(bc2_pin, OUTPUT);  digitalWrite(bc2_pin, LOW); //switch off the servo boost converter
  pinMode(sink_pin, OUTPUT); digitalWrite(sink_pin, LOW);
  pinMode(mos1_pin, OUTPUT); digitalWrite(mos1_pin, HIGH); //keep the mosfets off for the time being (rather than floating)
  pinMode(mos2_pin, OUTPUT); digitalWrite(mos2_pin, LOW);
  pinMode(mute_pin, OUTPUT); digitalWrite(mute_pin, HIGH); //set low to mute the amp
  pinMode(led1_pin, OUTPUT);
  pinMode(led2_pin, OUTPUT);


  pinMode(ldr_pin, INPUT);
  pinMode(battery_pin, INPUT);
  pinMode(charge_pin, INPUT);
  pinMode(bagSwitch_pin, INPUT);

  //neopixel begin
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'


  strip.setPixelColor(0, 100, 50, 10);
  strip.setPixelColor(1, 20, 200, 30);
  strip.setPixelColor(2, 200, 20, 100);
  strip.show();
}



void loop() {



  analogWrite(led1_pin, 50);
  analogWrite(led2_pin, 100);



  strip.setPixelColor(0, 100, 50, 100);
  strip.setPixelColor(1, 0, 50, 20);
  strip.setPixelColor(2, 200, 100, 50);
  strip.show();




  ldr_val = analogRead(ldr_pin);
  //battery reading
  battery_val = analogRead(battery_pin);
  //charge status
  charge_state = digitalRead(charge_pin);
  //bagSwitch status
  bagSwitch_val = analogRead(bagSwitch_pin);

  battery_voltage = map(battery_val, 0, 1023, 0, 3284);
  battery_voltage = map(battery_voltage, 0, 3284, 0, 4200);


}

Well I have run that code and get no flashing at all on pin 6. The LED on Pin 6 is just dimmed.

I have no other hardware connected other than that LED.

I wonder if you actually have a power supply issue rather than a software issue.

Grumpy_Mike:
Well I have run that code and get no flashing at all on pin 6. The LED on Pin 6 is just dimmed.

I have no other hardware connected other than that LED.

I wonder if you actually have a power supply issue rather than a software issue.

well thanks very much for taking the time to try out the code. if it is indeed a power supply issue, how could disabling the various processes i listed affect this?

how could disabling the various processes i listed affect this?

Because they then would not draw any current, like the Neopixels would not be on. So it could be insufficient current capacity, or insufficient supply decoupling, or interference, or earth lift due to incorrect wiring layout.

Hi,

Do you have a DMM, to measure some voltages for us?
What is your power supply?

Thanks.. Tom.. :slight_smile: