Go Down

Topic: PWM issue with pin 11 (Read 205 times) previous topic - next topic

Pegase_Stacky

Apr 30, 2016, 10:06 pm Last Edit: May 01, 2016, 01:13 pm by Pegase_Stacky
Dear all,

I've bought recently the Arduino Starter kit and I am currently working around the project No. 6 which explains the tone() function.

I wanted to connect my piezo either to one pin with analogWrite() or to another pin with Tone().
I could switch on/off these pins with a push button and I could have heard the difference.

Moreover, just to play around with the LED, I wanted a LED to be connected to another pin with analogWrite() all the time.

So I created the attached "Arduino Uno V1" sketch.

But strangely, when the piezo receives the signal the Tone() (Pin 2), the analogWrite () from the pin 11 doesn't work, when paradoxically it works when the piezo receives the signal from analogWrite() (pin 3).

I tried then to replace LEDs, etc... to check if the problem was not related to the the resistor or the blue LED but when I changed pin 11 to pin 5, everything works perfectly, whatever is the signal going to the piezo. (See attached file "Arduino Uno V2" sketch).

Do you have any idea why it does that with pin 11?


Below you can find my code.

Thanks in advance for your help.

Eric


CODE:

Code: [Select]

const int redLedPin = 10;    // Pin definitions
const int blueLedPin = 11;
const int blueLedSparePin = 5;
const int blueLedTestPin = 12;  //Green cable
const int greenLedPin = 7;
const int yellowLedPin = 6;
const int photoResPin = A0;
const int pushButtonPin = 4;
const int pushButtonTestPin = 8;
const int buzzerTonePin = 2;
const int buzzerPWMPin = 3;

const int initTime = 10000;  // Time for initialization phase

int sensorValue;             // Variables definition
int sensorLow = 1023;        // Variables for calibration
int sensorHigh = 0;
int buttonPreviousSatus;
int buttonCurrentStatus;
int buzzerStatus;
int buzzerTest;



void setup() {
buttonPreviousSatus = LOW;
buttonCurrentStatus = LOW;
buzzerTest = LOW;
buzzerStatus = buzzerTonePin;

pinMode(redLedPin, OUTPUT);
pinMode(blueLedPin, OUTPUT);
pinMode(blueLedTestPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(buz// May be incorrectzerTonePin, OUTPUT);
pinMode(buzzerPWMPin, OUTPUT);
pinMode(pushButtonPin, INPUT);
pinMode(pushButtonTestPin, INPUT);


digitalWrite(blueLedPin, LOW);
digitalWrite(blueLedTestPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(yellowLedPin, LOW);
digitalWrite(redLedPin, HIGH);

digitalWrite(buzzerPWMPin, LOW);
noTone(buzzerTonePin);
 
Serial.begin(9600);
Serial.println("Calibration's started:");
Serial.println();

// Initialization loop
while (millis()<initTime){
 sensorValue = analogRead(photoResPin);
 Serial.print("Sensor LOW: ");
 Serial.print(sensorLow);
 Serial.print(", Sensor HIGH: ");
 Serial.println(sensorHigh);
 
 if (sensorValue > sensorHigh){
   sensorHigh = sensorValue;
 }
 if (sensorValue < sensorLow){
   sensorLow =  sensorValue;
 }  
}

digitalWrite(redLedPin, LOW);       //Switch Off Red LED when calibration is done
digitalWrite(blueLedTestPin, HIGH); //Switch On the reference Blue LED
}


void loop() {
sensorValue = analogRead(photoResPin);

// Calibration check
if (sensorValue > sensorHigh){
   sensorHigh = sensorValue;
 }
if (sensorValue < sensorLow){
   sensorLow =  sensorValue;
 }

//Frequency for tone function
int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);      
//PWM for blueLedPin / buzzerPWMPin
int blueLedValue = map(sensorValue, sensorLow, sensorHigh, 0, 255);
//Tension for blueLedPin
float blueLedU = blueLedValue/255.0*5.0;
Serial.print("pitch: ");
Serial.print(pitch);
Serial.print(" Hz, Blue LED / Yellow Buzzer Value: ");
Serial.print(blueLedValue);  
Serial.print(", Blue LED Tension Value: ");
Serial.println(blueLedU);


buttonCurrentStatus = digitalRead(pushButtonPin);
while(buttonCurrentStatus == HIGH){
 buttonPreviousSatus = HIGH;
 buttonCurrentStatus = digitalRead(pushButtonPin);
}
if(buttonPreviousSatus == HIGH){
 buttonPreviousSatus = LOW;
 if(buzzerStatus == buzzerTonePin){
   buzzerStatus = buzzerPWMPin;
 }
 else{
   buzzerStatus = buzzerTonePin;
 }
}


// Blue LED swtiched on
analogWrite(blueLedPin, blueLedValue);
analogWrite(blueLedSparePin, blueLedValue);
analogWrite(redLedPin, blueLedValue);

if(buzzerStatus == buzzerTonePin){
 digitalWrite(buzzerPWMPin, LOW);
 digitalWrite(yellowLedPin, LOW);
 digitalWrite(greenLedPin, HIGH);
 tone(buzzerTonePin, pitch);
}
else{
 noTone(buzzerTonePin);
 digitalWrite(greenLedPin, LOW);
 digitalWrite(yellowLedPin, HIGH);
 analogWrite(buzzerPWMPin, blueLedValue);
}

delay(10);

buzzerTest = digitalRead(pushButtonTestPin);
while(buzzerTest == HIGH){
 buzzerTest = digitalRead(pushButtonTestPin);
 noTone(buzzerTonePin);
 digitalWrite(greenLedPin, LOW);
 digitalWrite(yellowLedPin, HIGH);
 analogWrite(buzzerPWMPin, 255);
 analogWrite(blueLedPin, 255);
}
buzzerTest = LOW;

delay(10);

}




DrAzzy

http://www.arduino.cc/en/Reference/Tone

Tone interferes with pwm on pins 3 and 11, because they use the same timer. I think you can get better results from tone on those pins too (because it does it like hardware pwm).
ATtiny core for 841+1634+828 and x313/x4/x5/x61/x7/x8 series Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts (some assembled), mosfets and awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Pegase_Stacky

Dear DrAzzy,

Thank you very much for your answer!

Is there somewhere a list of all the interferences that are known between pins and the functions used?

Best regards,

Eric

septillion

Yes, it's called "the datasheet" ;)

But as a quick reference you can use one of those Arduino pinout images:

But it doesn't show all.

And just read the reference/documentation of a library. If you did that you would have read:
Quote
Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Pegase_Stacky

Dear Septillion,

Thank you for your answer and as you said, I  should have read better the reference/documentation of the tone() function.

However, how can you see of the picture you provided me that there would have been and interference between this function and this 11th pin?

If you can't see it on this document, what kind of incompatibility can you detect on this document? Can you give me an exemple?

Thanks in advance,

Cheers,

Eric

septillion

The image doesn't show that indeed. It does not show to which timer each PWM is connected. For that you do need to read the reference of a library and that has to be good, not all (third party) library references are that great... Then you're just out of luck (aka it's a crap written library).

But you can sport other issues. Like serial will interfere with pin0 and pin1. Or I2C with A4 and A5. etc
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Pegase_Stacky

Hi Septillion,

Thanks a lot for your feedback! Duly noted!

Cheers,

Eric

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy