0

I have this question and I can't find answers!

I wanted to use an AtTiny45V (due to low voltage requirements, it will run at 1.8 volts) to read a PWM from an Arduino, and then process this information.

Explained better:

let's say, I hook up one pin of the AtTiny to an Arduino pin (named connection), and in the IDE I write:

AnalogWrite(connection,X)

I want the AtTiny to write high on a port based on the X value.

Example:

X = 20 writes high on AtTiny PB5

X = 25 writes high on AtTiny PB5 and PB3

etc...

So, basically I want to read the duty cycle from the Arduino PWM and let the AtTiny do something based on duty cycle value.

Is this possible?

Thanks

migrated from electronics.stackexchange.com May 25 '17 at 22:19

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • What is the switching frequency of your pwm? How fast do you want to respond? – gregb212 May 25 '17 at 16:06
  • I'll be using the default PWM frequency of Arduino (490Hz or 980Hz, pin-dependant). For the response time I don't need it to respond in real-time, but also not taking ages for responding! – Ghesio May 25 '17 at 16:10
  • Your question is confusing though. You say you want your secondary mcu (the attiny) to set a pin high when the primary mcu (arduino) sets a pin high, but then you go on to say you want to measure duty cycles. One is just mirroring a pin state, the other is measuring the pin state over time. The former is much easier because time isn't part of the equation. – gregb212 May 25 '17 at 16:13
  • 2
    @Ghesio take a look at the AVR135 app note from Atmel - atmel.com/Images/… – Todor Simeonov May 25 '17 at 17:10
  • 1
    @Gerben That is not a problem, I have only 8 cases to choose (I have to switch 3 leds, basically I'm doing an RGB controller), so a big accuracy is not required (I could make in the if controlling on the input pin a +/-10 error tollerance). – Ghesio May 26 '17 at 15:42
1

You don't need to read the duty cycle, just the time that the PWM signal is high. First wait for the input pin to go high, then start a timer, wait for the signal to go low again, and finally read the timer value. If other tasks need to be performed concurrently then use pin change interrupt to detect the high and low transitions.

If the ATtiny is running Arduino software then the solution is even easier - just use the pulseIn() function.

This technique works for all PWM values except 0 and 255. With these values the pin stays low or high permanently, so there are no transitions to time. The easiest way around this is to simply avoid transmitting 0 or 255.

Alternatively you could pass the PWM signal through an RC filter to produce a 'DC' voltage, then read it with the ADC. However this will slow the response time down, and the signal will produce intermediate values as it goes from one voltage to another. To get good resolution the RC filter would need to have a long time constant, and you might have to average several readings to get a stable value.

  • Well, thanks. I'll try this as soon as possible, it should work – Ghesio May 26 '17 at 12:04
  • It's working like a charm. I marked only now because I had to mill the board and solder everything! If someone is interested I'm documenting this "PWM controller", just contact me. – Ghesio May 30 '17 at 15:14
0

If I understood correctly you want to drive a digital output on the AtTiny45V based on the PWM value from your Arduino, right?

I would suggest to use a simple low-pass RC filter to extract the mean value from the PWM and read as an analog voltage in the AtTiny45V. As the frequency of your PWM is known, you can easily design your filter to extract the mean value of your signal. Then, based on your PWM mean value, you can define which pin will be set.

  • The PWM frequency is very low - about 500Hz. The filter will have such a big R*C that he should read it analog once in a minute. Not a very good idea. – Todor Simeonov May 25 '17 at 17:47
  • I don't think it's that big, assuming the 500Hz, set the filter to 250 Hz, choosing C = 0.1uF, then R = 6.2k, this will give a time constant of 0.0006s, so the rise time will be around 0,003s. Please, correct me if I'm wrong on this. I used this method once, but the PWM frequency was 5kHz and it was a long ago. – T Borges May 25 '17 at 17:57
  • these values with a 5V 50% PWM at 500Hz give a triangular (sawtooth) waveform with Vmin=0.8V, Vmax=4.2V. - PSPICE simulation results, I'm not joking :) R*C needs to be much bigger. – Todor Simeonov May 25 '17 at 18:02
  • @TodorSimeonov, You're absolutely right, I was thinking about a step input to the filter instead of a square wave. Thanks to clarify my proposition. – T Borges May 25 '17 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.