ATTINY85 question - Page 1
Forum section: electronics / general

author: Wawavoun
author_rank: Regular Contributor
timestamp: January 30, 2021, 10:47:54 pm
content: 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 whyAny help greatly appreciated.RegardsPhilippe#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 LEDconst int ANTENNA = 2; // this is physical pin 7, connect wire as antennaconst int Rx = 3; // this is physical pin 2const int Tx = 4; // this is physical pin 3SoftwareSerial mySerial(Rx, Tx);int val = 0; // variable to store antenna readingsvoid setup(){pinMode(LED, OUTPUT); // tell LED is an outputpinMode(ANTENNA, INPUT); // tell ANTENNA is an inputmySerial.begin(9600); // send serial data at 9600 bits/sec}void loop(){digitalWrite(LED, HIGH); // turn LED ONdelay(500);digitalWrite(LED, LOW); // turn offdelay(500);val = analogRead(ANTENNA); // read the ANTENNAmySerial.println("Test");mySerial.println(val); // send the value to Serial Monitor, ^Cmd-MdigitalWrite(LED, HIGH); // turn LED ONdelay(100);digitalWrite(LED, LOW); // turn offdelay(500);}

author: sleemanj
author_rank: Super Contributor
timestamp: January 30, 2021, 11:47:04 pm
content: 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.

author: Syntax Error
author_rank: Frequent Contributor
timestamp: January 31, 2021, 12:41:08 am
content: @Wawavoun To set a clock speed in code, you may need to redfine the F_CPU value. For example, at the start:Code:#ifdef F_CPU#undef F_CPU#define F_CPU 8000000L#endifNote the frequency is a long number.For calculating all fuse values, so you can set them manually with AVRDUDE, use theEngbedded Atmel AVR® Fuse Calculatorhttps://www.engbedded.com/fusecalc/

author: Wawavoun
author_rank: Regular Contributor
timestamp: January 31, 2021, 09:58:39 am
content: 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

author: brucehoult
author_rank: Super Contributor
timestamp: January 31, 2021, 08:41:15 pm
content: Quote from: Syntax Error on January 31, 2021, 12:41:08 amShirley you mean ...Code:#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:#undef F_CPU#define F_CPU 8000000L