Problem with ADC with an Attiny85

Thread Starter

jonny2612

Joined Jan 24, 2017
4
Am trying to learn more about ADC. Using Arduino uno as SPI to program an Attiny85. Currently having problems getting my code to work. would appreciate any help I would get.
Code:

Code:
const int soilhalfwet = 512; // Half value of the analog input total is 1023
float analogResult = 0. ; //Place holder for the data received from sensor

int sensorValue = PB4; // Input on pin 3 of attiny for adc sensor int sensorparameter = 0; // variable place holder
int Mgate = PB1; //Output on pin 6 that controls the mosfet

void setup() {

// put your setup code here, to run once:

//Serial.begin(9600);

pinMode(sensorValue, INPUT);      // sets the pin as input
pinMode(Mgate, OUTPUT);           // sets the pin as output

  setupADC();                   

}

void loop() {

    startConversion();

    while (ADCSRA & (1<<ADSC)); // wait for conversion to complete

    analogResult = (ADCH << 8)| ADCL; // save results to analogresult variable
  
   if (analogResult >= soilhalfwet) // checks if analogresult is greater or equal to 512
    {
     digitalWrite(Mgate, HIGH); // Turn on
     delay (1000);
     digitalWrite(Mgate, LOW); // Turn off
    }     
   else
     {
         digitalWrite(Mgate, LOW); // Turn off
     }
  
}

void setupADC()
{
  ADCSRA = (1 << ADEN );                  // ADC Enable
  ADMUX &= ~((1 << REFS1)| (1 << REFS0)); // using Vcc and GND as references
  ADMUX |= (1 << ADLAR);                  // ADLAR set to 0
  ADMUX= (1 << MUX1);                    // choosing PB4 channel
// ADCSRA = (1 << ADIE);                 //Interrupt enable
  ADCSRA = (1 << ADPS2)| (1 << ADPS1)| (1 << ADPS0);   //sets prescaler to 128 (8*10^6/128 = 62,5Khz)
}

void startConversion()
{
   ADCSRA |= (1 << ADSC);
}
Moderator edit: added code tags
 

Thread Starter

jonny2612

Joined Jan 24, 2017
4
It might help if you describe the problem you are experiencing.
After abit of reseach and changing the order of register in the setADC (), i got it working but the output is always high, seems the if statement is always true.

this was the modification i did in the setADC ()

Code:
void setupADC()
{
  ADMUX &= ~((1 << REFS1)| (1 << REFS0)); // using Vcc and GND as references
// ADMUX &=~(1 << ADLAR);                  // ADLAR set to 0 default
  ADMUX= (1 << MUX1);                    // choosing PB4 channel
  ADCSRA = (1 << ADPS2)| (1 << ADPS1)| (1 << ADPS0); //sets prescaler to 128 (8*10^6/128 = 62,5Khz)
  ADCSRA = (1 << ADEN );                  // ADC Enable
}
Moderator edit: added code tags
 

Thread Starter

jonny2612

Joined Jan 24, 2017
4
The problem it seems that the if statement is true the whole time but here is what I dont understand !! If the if statement is true it should set output to high, then wait for some time as per the delay then set it to low. This is not happening even after i played around with different delay times.
 

RayB

Joined Apr 3, 2011
32
If you wish to start with a similar analog/PWM project for a t85 that is know to work, you can try mine here:
https://www.hackster.io/rayburne/hot-yet-8d097d

The above has many of the same elements of your project. Examine how easy it is to set-up the ADC and how I handle the sleep aspects of the uC. I'm off in ESP land these days and have not played with AVR's in a couple of years.

More projects here: https://www.hackster.io/rayburne/projects
Remember, code written for Arduino is like a snapshot in time .... the code stays the same but the Arduino environment moves forward. So, I guarantee nothing with the newest ArduinoIDE ... you may have to drop back to an old IDE to run my projects or you can feel free to make whatever changes are necessary based on the newer compiler/linker versions and IDE flags passed to the underlying tools.

Ray
 
Top