I am getting started with writing programs for Adruino. I was successfully able to implement the LED-hello-world of putting together an LED, a 220 Ohm resistor on a bread board and wiring it to my UNO Q.
The project is pretty simple, there's no reason why it shouldn't work.
Using Wokwi it works perfectly, the LED turns on and off gradually so either you have a faulty LED or some other type of hardware problem (e.g. defective breadboard or contacts inserted incorrectly or in the wrong hole...).
Forgive me, but I still don't understand. Are you telling me that the call to analogWrite() doesn't work if the pin is declared OUTPUT (which should be a requirement), despite the fact that it defaults to INPUT? I've never heard of this, so I'm curious. Can you tell me where you read it?
Just out of curiosity and to try to understand: is this a "feature" or a "bug" in the UNO Q platform/compiler? Because in my opinion, it makes no sense for it to behave differently from other Arduino boards.
Thanks. So, since this behavior seems different from other Arduinos, can you confirm that it's a specific behavior of the UNO Q (and that in my opinion it's an unacceptable "asymmetry")?
And in any case, why does using pinMode(PWMPin, OUTPUT) prevent correct operation?
Yes, @GolamMostafa, thank you but I already know we're talking about UNO Q.
I'm just asking/wondering why on UNO Q using pinMode() prevents analogWrite() from working, as it has no reason at all in my opinion, and it appears to me to be something that violates the uniformity of behavior of Arduino boards.
I believe pinMode(9, OUTPUT); prevents analogWrite() from working because pinMode() is for configuring a pin as digital IO rather Analog. I speculate that the digital block for a given pin must be in a high impedance state for the analog circuit to have any effect on the pin. When pinMode(9, OUTPUT); is used the digital block will overwrite the analog circuit.
I suspect this has not come up on other Arduino boards you have used simply because earlier Uno boards do not have true digital to analog converters. PWM is a digital function that uses duty-cycle to provide an time averaged effect similar to analog but look quite different on an oscilloscope.
I believe I have used the Arduino IDE with non Arduino boards that have an anolog output which would be configured the same way the Uno Q is. I do not think this is something new or unique to the Uno Q.
Ok, that's what I was worried about. And this confirms my revulsion for the UNO Q (or, rather, for this Qualcomm "invasion" of a decade-old Arduino ecosystem) as an "anomaly" that I don't consider positive. Personal opinion, of course.
Probably, but for me it's yet another confirmation that the Q's "core" is not correct. I hope they do something to ensure that the UNO Q's basic behavior and functionality are always consistent with the rest of the boards.
PWM signal is essentially a digital signal with "varying duty cycle" and NOT a "slowly varying DC signal" (aka analog signal). So, use of pinMode(9, OUTPUT) is fully justified.
If we look into #12 for the implementation of analogwrite() function, the following line is there, which is the register level code of pinMode(9, OUTPUT).
`DDRB |= (1 << 1); //DPin-9 (PB1/OC1A) as output `
In the above code, the Bit-1 of Data Direction Register of Port-B is made HIGH so that the direction of the correesponding DPin-9 is output.
When we do bit manipulation of TC1 registers of UNOR3 to generate Fast Mode PWM signal, we need to reset TCCR1A = 0 and TCCR1B = 0 first; otherwise, the codes do not generate the signal. It is because the init() function (executed before setup()) might have put some non-zero values into some bits of TCCR1A and TCCR1B registers, which have pevented our newly created codes from working.
void init()
{
// this needs to be called before setup() or some functions won't
// work there
sei();
// on the ATmega168, timer 0 is also used for fast hardware pwm
// (using phase-correct PWM would mean that timer 0 overflowed half as often
// resulting in different millis() behavior on the ATmega8 and ATmega168)
sbi(TCCR0A, WGM01);
sbi(TCCR0A, WGM00);
// set timer 0 prescale factor to 64
// this combination is for the standard 168/328/1280/2560
sbi(TCCR0B, CS01);
sbi(TCCR0B, CS00);
// enable timer 0 overflow interrupt
sbi(TIMSK0, TOIE0);
// timers 1 and 2 are used for phase-correct hardware pwm
// this is better for motors as it ensures an even waveform
// note, however, that fast pwm mode can achieve a frequency of up
// 8 MHz (with a 16 MHz clock) at 50% duty cycle
TCCR1B = 0;
// set timer 1 prescale factor to 64
sbi(TCCR1B, CS11);
sbi(TCCR1B, CS10);
// put timer 1 in 8-bit phase correct pwm mode
sbi(TCCR1A, WGM10);
// set timer 2 prescale factor to 64
sbi(TCCR2B, CS22);
// configure timer 2 for phase correct pwm (8-bit)
sbi(TCCR2A, WGM20);
// set a2d prescale factor to 128
// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
// XXX: this will not work properly for other clock speeds, and
// this code should use F_CPU to determine the prescale factor.
sbi(ADCSRA, ADPS2);
sbi(ADCSRA, ADPS1);
sbi(ADCSRA, ADPS0);
// enable a2d conversions
sbi(ADCSRA, ADEN);
// the bootloader connects pins 0 and 1 to the USART; disconnect them
// here so they can be used as normal digital i/o; they will be
// reconnected in Serial.begin()
UCSR0B = 0;
}