Author Topic: Arduino PWM signal acquisition dubt  (Read 635 times)

0 Members and 1 Guest are viewing this topic.

Offline Lucky-LukaTopic starter

  • Supporter
  • ****
  • Posts: 240
  • Country: it
Arduino PWM signal acquisition dubt
« on: February 20, 2020, 07:24:27 pm »
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.
Cheers
https://youtu.be/4YkDUCK050s
« Last Edit: February 20, 2020, 08:16:28 pm by Lucky-Luka »
Memento audere semper.
 

Offline MasterT

  • Frequent Contributor
  • **
  • Posts: 793
  • Country: ca
Re: Arduino PWM signal acquisition dubt
« Reply #1 on: February 20, 2020, 09:37:52 pm »
Pins 9 & 10 belong to Timer-1, and pin 11 to Timer-2.  Two timers are not synchronized, so phase offset could be anything. 
 
The following users thanked this post: Lucky-Luka

Offline Lusu

  • Contributor
  • Posts: 21
  • Country: ro
Re: Arduino PWM signal acquisition dubt
« Reply #2 on: February 21, 2020, 06:59:49 am »
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.
 
The following users thanked this post: Lucky-Luka

Offline Lucky-LukaTopic starter

  • Supporter
  • ****
  • Posts: 240
  • Country: it
Re: Arduino PWM signal acquisition dubt
« Reply #3 on: February 21, 2020, 09:15:23 am »
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.
Memento audere semper.
 

Offline Lucky-LukaTopic starter

  • Supporter
  • ****
  • Posts: 240
  • Country: it
Re: Arduino PWM signal acquisition dubt
« Reply #4 on: February 21, 2020, 09:54:03 am »
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
« Last Edit: February 21, 2020, 09:58:34 am by Lucky-Luka »
Memento audere semper.
 

Offline Lusu

  • Contributor
  • Posts: 21
  • Country: ro
Re: Arduino PWM signal acquisition dubt
« Reply #5 on: February 21, 2020, 10:31:51 am »
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, delay
TIMER 1 (16 bit) is controlling PWM on pins 9 and 10
TIMER 2 (8 bit) is controlling PWM on pins 3 and 11 and is used by Tone

You 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.
 
The following users thanked this post: Lucky-Luka

Offline Lucky-LukaTopic starter

  • Supporter
  • ****
  • Posts: 240
  • Country: it
Re: Arduino PWM signal acquisition dubt
« Reply #6 on: February 21, 2020, 10:39:29 am »
In the attached pic there is my setup.

Code: [Select]
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);
}
« Last Edit: February 21, 2020, 10:50:03 am by Lucky-Luka »
Memento audere semper.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf