Author Topic: ATTINY85 question  (Read 1690 times)

0 Members and 2 Guests are viewing this topic.

Offline WawavounTopic starter

  • Regular Contributor
  • *
  • Posts: 117
  • Country: fr
  • Someday I'll get you, Red Baron !
ATTINY85 question
« on: January 30, 2021, 10:47:54 pm »
Hello there !

I have two questions about the code hereunder.

I try to do some basic test with an ATTiny85, Arduino IDE and usbasp programmer.

First the serial com only works if I set the Arduino IDE with 1 Mhz internal clock.
Did I have to change something in the code if I want to use 8 MHz or 16 MHz internal clock ?

I have assumed that the library will do that but obviously no...

Second : the value read at pin 7 an send by mySerial.println(val) stay constantly at 1023... Even If I connect pin 7 to GND or to +5V !!!
And I dont understand why ???

Any help greatly appreciated.
Regards
Philippe



#include <SoftwareSerial.h>

// For ATtiny85 : turn on led, analog read and serial com

// RESET | pin 1 pin 8| 5V
// 3/A3 | pin 2 pin 7| 2/A1
// 4/A2 | pin 3 pin 6| 1/PWM
// GND | pin 4 pin 5| 0/PWM

// for SoftwareSerial you must set up as internal clock // to free up physical pins 2 and 3.
// This will flash an LED on physical pin 6, read the analog value on pin 7, and send the
// analog value to the Serial Monitor for display.

const int LED = 1; // this is physical pin 6 for the LED
const int ANTENNA = 2; // this is physical pin 7, connect wire as antenna
const int Rx = 3; // this is physical pin 2
const int Tx = 4; // this is physical pin 3

SoftwareSerial mySerial(Rx, Tx);

int val = 0; // variable to store antenna readings

void setup()
{
pinMode(LED, OUTPUT); // tell LED is an output
pinMode(ANTENNA, INPUT); // tell ANTENNA is an input
mySerial.begin(9600); // send serial data at 9600 bits/sec
}

void loop()
{
digitalWrite(LED, HIGH); // turn LED ON
delay(500);
digitalWrite(LED, LOW); // turn off
delay(500);
val = analogRead(ANTENNA); // read the ANTENNA
mySerial.println("Test");
mySerial.println(val);  // send the value to Serial Monitor, ^Cmd-M
digitalWrite(LED, HIGH); // turn LED ON
delay(100);
digitalWrite(LED, LOW); // turn off
delay(500);
}
 

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3176
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: ATTINY85 question
« Reply #1 on: January 30, 2021, 11:47:04 pm »
You must set appropriate fuses to make the chip operate on the 8MHz internal oscillator.  Most likely your 85 is still set (as per factory) to divide by 8 therefore 1MHz.

In Arduino IDE this will depend on core, but if your core is capable then select appropriate processor options under Tools menu and Burn Bootloader to set fuses, take care not to choose wrong options that would set fuses for external clock source.

You should use the appropriate "Ax" define for the analogRead (and pinMode) as defined by your core, "7" or "2" will undoubtedly mean something different than you expect.

Eg if you are using SpenceKonde's ATTinyCore or my fork thereof then you would use analogRead(A1) to read from physical pin 7. 

« Last Edit: January 30, 2021, 11:50:50 pm by sleemanj »
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 
The following users thanked this post: Wawavoun

Offline Syntax Error

  • Frequent Contributor
  • **
  • Posts: 584
  • Country: gb
Re: ATTINY85 question
« Reply #2 on: January 31, 2021, 12:41:08 am »
@Wawavoun To set a clock speed in code, you may need to redfine the F_CPU value. For example, at the start:
Code: [Select]
#ifdef F_CPU
#undef F_CPU
#define F_CPU 8000000L
#endif
Note the frequency is a long number.

For calculating all fuse values, so you can set them manually with AVRDUDE, use the Engbedded Atmel AVR® Fuse Calculator
https://www.engbedded.com/fusecalc/

 
The following users thanked this post: Wawavoun

Offline WawavounTopic starter

  • Regular Contributor
  • *
  • Posts: 117
  • Country: fr
  • Someday I'll get you, Red Baron !
Re: ATTINY85 question
« Reply #3 on: January 31, 2021, 09:58:39 am »
Thank you very much !

With correct fuses settings serial com works like a charm.
Also A1 was the good input name for analog reading on pin 7.

Regards.
Philippe
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6464
  • Country: nz
Re: ATTINY85 question
« Reply #4 on: January 31, 2021, 08:41:15 pm »
@Wawavoun To set a clock speed in code, you may need to redfine the F_CPU value. For example, at the start:
Code: [Select]
#ifdef F_CPU
#undef F_CPU
#define F_CPU 8000000L
#endif

Shirley you mean ...

Code: [Select]
#ifdef F_CPU
#undef F_CPU
#endif
#define F_CPU 8000000L

... as you want to have F_CPU defined afterwards, no matter whether it was already defined or not.

But in fact the following will do fine, as #undef doesn't error if it was not actually defined.

Code: [Select]
#undef F_CPU
#define F_CPU 8000000L
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->