Arduino PWM signal acquisition dubt - Page 1
Forum section: electronics / beginners

author: Lucky-Luka
author_rank: Unknown
timestamp: February 20, 2020, 07:24:27 pm
content: I'm doing a little project in order to learn to use Arduino.In the attached image it's shown the circuit.3 phototransistors with 3 colored filters let a RGB LED shine accordingly to che color of the light that hit the three sensors.I have connected my probes to the pins 11 (RED), 10 (BLUE) and 9 (GREEN).Those are the 3 traces displayed on the oscilloscope screen. I change light conditions sequentially (RED light then GREEN light then BLUE light) and I can see that the duty cycle of the 3 signals (RED, GREEN, BLUE) change accordingly.The red trace (PIN 11) is the one the trigger of the scope is referred to.Its rising edge is fixed on the screen while its falling edge moves accordingly to the power supplied by the probed PIN.When the power delivered to the PIN 10 and 9 changes looking at the GREEN and BLUE traces I notice that not only the corresponding falling edge move on the screen but even the rising edges move.In another moment I have set the trigger for the GREEN trace: in this case only the GREE trace rising edge is fixed.I have even set the trigger for the BLUE trace and I have noticed that only the BLUE trace rising edge is fixed.Can sonmeone tell me what's going on?I was expecting to see all the three rising edges traces in fixed positions on the screen.I have to say that I'm still learning to use the scope so maybe I haven't understood something related to its settings.Cheershttps://youtu.be/4YkDUCK050s

author: MasterT
author_rank: Frequent Contributor
timestamp: February 20, 2020, 09:37:52 pm
content: Pins 9 & 10 belong to Timer-1, and pin 11 to Timer-2. Two timers are not synchronized, so phase offset could be anything.

author: Lusu
author_rank: Contributor
timestamp: February 21, 2020, 06:59:49 am
content: More over, Timer 2 is 16 bits while Timer 1 is 8 bits...You need to trigger independently each channel, or probe pin 11 individually.

author: Lucky-Luka
author_rank: Unknown
timestamp: February 21, 2020, 09:15:23 am
content: Very interesting.I think I need to read the ATmega328P datasheet.It looks like that behind the curtain nothing is so straightforward as what the Arduino high level language induced me to think.

author: Lucky-Luka
author_rank: Unknown
timestamp: February 21, 2020, 09:54:03 am
content: In the attached pic it's shown what happens when the blue signal is triggered and I use blue light.The duty cycle of the blue trace increases while both the green and red traces just shift to the right.If pin 9 (GREEN) and pin 10 (BLUE) refer to the same timer shouldn't those traces have the same position for the rising edges?Cheers

author: Lusu
author_rank: Contributor
timestamp: February 21, 2020, 10:31:51 am
content: Small typo in my comment above... Timers 0 & 2 are 8 bits, 1 is 16 bits.TIMER 0 (8 bit) is controlling PWM on pins 5 and 6 as well as millis, micros, delayTIMER 1 (16 bit) is controlling PWM on pins 9 and 10TIMER 2 (8 bit) is controlling PWM on pins 3 and 11 and is used by ToneYou need to check the code for what the actual values are sent to PWM... it seems that you depend on photo elements, this is uncontrollable in your case. Maybe you should try a simple program that outputs specific values on those pins (using analogWrite which is probably what you use) and then check the output.Anyway, don't assume they will be in sync... internal circuits are quite complicated. Maybe singleshot will be more correct than live update.

author: Lucky-Luka
author_rank: Unknown
timestamp: February 21, 2020, 10:39:29 am
content: In the attached pic there is my setup.Code:const int greenLEDPin=9;const int blueLEDPin=10;const int redLEDPin=11;const int redSensorPin=A0;const int greenSensorPin=A1;const int blueSensorPin=A2;int redValue=0;int greenValue=0;int blueValue=0;int redSensorValue=0;int greenSensorValue=0;int blueSensorValue=0;void setup() {Serial.begin(9600);pinMode(greenLEDPin,OUTPUT);pinMode(blueLEDPin,OUTPUT);pinMode(redLEDPin,OUTPUT);}void loop() {redSensorValue=analogRead(redSensorPin);delay(5);greenSensorValue=analogRead(greenSensorPin);delay(5);blueSensorValue=analogRead(blueSensorPin);Serial.print("Raw sensor values \t red: ");Serial.print(redSensorValue);Serial.print("\t green: ");Serial.print(greenSensorValue);Serial.print("\t blue: ");Serial.println(blueSensorValue);// Signal values (after ADC conversion: 0-1023) converted to PWM values (0-255)redValue=redSensorValue/4;greenValue=greenSensorValue/4;blueValue=blueSensorValue/4;Serial.print("Mapped sensor values \t red: ");Serial.print(redValue);Serial.print("\t green: ");Serial.print(greenValue);Serial.print("\t blue: ");Serial.println(blueValue);analogWrite(redLEDPin,redValue);analogWrite(greenLEDPin,greenValue);analogWrite(blueLEDPin,blueValue);}