ATTINY84 PWM

Hi

Wonder if anyone can guide me on a problem I have.

Migrating a project from the UNO to a standalone ATTINY84 I've encountered a problem with PWM.

I think Pin 6 and 7 on the ATTINY are the PWM (I've tried both) pins but in my program the motor won't turn on any value other than 255. Any other values, the motor fails to turn though I can here the motor 'click'.

The PWM signal is sent to Enable 1 on an L293D.

It worked fine on the UNO so I'm thinking a config problem on the smaller chip??

I'm using Arduino as an ISP and have tried both Internal processor speeds on the ATTINY - after burning the bootloader.

Everything works fine apart from being able to control the motor speed!

Appreciate any thoughts please?

Sorry, our telepaths don't work on Sundays. You'll have to post code...

Code attached below. Don't think this a coding issue - as it works on the UNO - with different pin assignments... the code is very BASIC! I'm new to all of this. Apols for the structure etc....

In case its not obvious the code drives a motor which raises and lowers a chicken house door dependent on light.

int photocellReading; // var for ldr reading
int sw_level = 340; // light level for switching

int doorstate = 0; // is the door open or closed 1=closed 0=open
int x = 1; // used to ensure motor keeps running once activated
int time = 0; // used to wait until after dark and not close immediately
int i=255; // motor speed 35 is slowest, 255 fastest
const int photocellPin = 0; // ldr on A5 - don't use A0 or A1 with motorshield.
const int doordownPin = 1; // door closed switch input pin
const int doorupPin = 2; // door open switch input pin
const int enable = 6; // motor pulse pin
const int forward = 4; // motor direction
const int reverse = 5; // motor direction
const int enable = 6; // motor pulse pin

void setup() {
pinMode(reverse, OUTPUT); // Initiates Motor Channel A pin
pinMode(forward, OUTPUT); // Initiates Brake Channel A pin
pinMode(doorupPin, INPUT); // Door Up Switch is an input
pinMode(doordownPin, INPUT); // Door Down Switch is an input

}

void loop() {
photocellReading = analogRead(photocellPin);
if ((photocellReading < (sw_level)) && (doorstate != 1)) {(time = time + 1);}
if (photocellReading >sw_level) (time = 0);
if (photocellReading > (sw_level)) {(doorup());} // goto doorup
if ((photocellReading < (sw_level)) && (doorstate != 1) && (time >= 3)) {doordown();} //goto door down
delay(20000);
}

void doorup(){
digitalWrite(reverse, LOW); //Set motor direction
digitalWrite(forward, HIGH); //Set motor direction
while (x=1) {
analogWrite(enable, i);
int pinState = digitalRead(doorupPin);
if (pinState == HIGH) {(digitalWrite(forward, LOW)); (doorstate = 0); break;} //stop
delay(25);
}
}

void doordown(){
digitalWrite(reverse, HIGH); //Set motor direction
digitalWrite(forward, LOW); //Set motor direction
while (x=1) {
analogWrite(enable, i);
int pinState = digitalRead(doordownPin);
if (pinState == HIGH) {(digitalWrite(reverse, LOW)); (time = 0); (doorstate = 1); break;} //stop
delay(25);
}
}

ignore the two lines with

const INT enable = pin 6;

cut'n'paste error on my part

Might be other problems but, "enable" needs to be set as OUTPUT.

cyclegadget:
Might be other problems but, "enable" needs to be set as OUTPUT.

Additionally, "enable" is always set to 255 after either of the doorup() or doordown() function calls. It's never set to anything but "i", and "i' isn't set to anything but the default 255. If it doesn't need to be anything else, ever, you could analogWrite(enable, i); in init() after you set it to output.

Cheers ! Geoff

Thanks for the input....

I tried setting enable as an OUTPUT

pinMode(enable, OUTPUT);

no change. I didn't think PWM pins needed to be set.

On the other suggestion....

i = 255. I want to be able to vary this value to set an optimum speed for the motor. The problem is that only i = 255 works! Any other value doesn't. This is the value that is send to the 'enable' Pin to set the pulse width.... I don't understand what you "in init ()" does?

Hi

D6 isn't capable of PWM on the ATTiny84 according to the pinout diagram I'm reading here. analogWrite(non-PWM-pin, 255) will result in a HIGH output, which is what you're seeing.

The issue I was mentioning about the enable pin being set to 255 is you do it inside two loops, repeatedly, but there's no need. It will stay at 255 until you change it and elsewhere in the code it is never set to anything else. For that reason I suggested moving it to the init() function where it will be run once and stay that way forever (regardless your choice of a value of i).

Hope that makes more sense.

Cheers ! Geoff

Thanks Geoff, I understand what you are saying re init() however the reference you give for the ATTINY84 suggests that PWM is available on those pins marked with an asterics...PA5,6,7 and PB2?

Just re-read your post. To clarify 'enable' isn't on D6 its on PA6 or PA7 - tried both. regards Roddy

Yes, that's perfectly true. And D6 is PA4...not one of those with an asterisk. If you're using the Arduino IDE to code for ATTiny, the pin numbers are the Dn & An references, just like on the Arduino.

Cheers ! Geoff

Hi Geoff

so the line of code

const int enable = 6; // motor pulse pin

should read what? and what physical pin should the wire be attached to on the ATTINY84?

Really appreciate your help.

regards Roddy

Hi Roddy

If you want PWM pin PA6 (physical microcontroller leg 7) , the line should read

const int enable = 4;         // motor pulse pin D4

PA7 would be D3. The PA reference is used if you are coding the low level AVR C, whereas Arduino abstracts the names further as digital or analog pin numbers.
The PA6 pin is addressable as either D4 or A6 in the Arduino IDE.

I suspect you want to check your other connections now too.

BTW I keep a print of that akafugu reference page in several places around my house. It's so useful !

Cheers ! Geoff

My problem just got a whole lot bigger!

In my haste to follow your last instruction I left the 'enable' wire disconnected from the Attiny48. So in theory without that pulse the motor won't run... guess what.. it still runs! So now I'm totally clueless. The L293D needs Enable 1 (pulsing to control speed) and either OUTPUT 1 or 2 HIGH to drive the motor.
Somehow or other I don't need to ENABLE and it still bloody runs.

Feeling totally stupid and lost now!

scot101:
In my haste to follow your last instruction I left the 'enable' wire disconnected from the Attiny48 ATtiny84. The L293D needs Enable 1 (pulsing to control speed) and either OUTPUT 1 or 2 HIGH to drive the motor.

Search the internet for "floating digital input" or this forum for "floating pin"...
https://www.google.com/search?q="floating"+pin+"digitalRead"+site:arduino.cc%2Fforum

Another way to look at it: The datasheet indicates enable (EN) must be driven LOW or HIGH. Anything else is then undefined. Which means the behaviour of the L293D is undefined.

Thank you Coding Badly - I should have remembered that.

Everything going very hazy as I try to work out the pin mapping from the ATtiny84 to the Arduino Uno...