How to add sensor to the following code

Hi, I have a code that uses 1.1Vref (because the system is powered by battery) (ATTINY44A IC). I have connected one sensor to it and now i would like to read value of the sensor. Please check the code below.

This code uses internal reference voltage and outputs unchanged voltage rating.(THE code is for arduino and with little changes it will work for attiny)


void loop() {  
  //REFS1 AND REFS0 to 1 1 -> internal 1.1V refference
  ADMUX |= B11000000;   
  //We read A1 (MUX0)
  ADMUX |= B00000001;       
  // Start AD conversion
  ADCSRA |= B11000000;
  // Detect end-of-conversion
  while (bit_is_set(ADCSRA,ADSC));
  float val = ADCL | (ADCH << 8);
  val = val * 5.7; 
  Serial.println(val);
}

from my perspective, in order to read sensor value the code should be:

int sensorVoltage = analogRead (A0);
  val = (val * 5.7);
  val = (val/1024);
  val = (val*sensorVoltage); 
  if (voltage<=2000){ //3V
   DO SOMETHING
  }

Unfortunately, the above code does not work as needed.

for example, if the sensor at 3v reads 2000 then it should be something like if (sensorvalue<=2000){do something};
But what if with some other battery little less or little more charged, the sensor does not read 2000 or reads some value greater than 2000 at 3v then what is the solution to it?

The value changes with battery voltage.

The code works alright with arduino but when i transfer it to attiny it does not work properly for instance, arduino reads 2000 at 3v whereas, attiny does not read 2000 at 3-3.4V.

The 328 and the 44 are different.
One thing I noticed is that REFS1,REFS0 should be set to 1,0 for the 44.
There may be several differences

I made it REFS1 =1 and REFS2= 0

the command becomes:

ADMUX |= B10000000;

correct?

 ADMUX |= B10000000;   // internal 1.1V refference
  //We read A1 (MUX0)
  ADMUX = 0b00000001;   //PA1   
  // Start AD conversion
  ADCSRA |= (1<<ADSC);
  // Detect end-of-conversion
  while (bit_is_set(ADCSRA,ADSC));
 val = ADCL | (ADCH << 8);

for my attiny

Correct

okay, thank you Mr jim-p.
The issue now is the code works ok with arduino, i can set any range but when it comes to attiny, that exact same range does not work. What might be the issue? is the code wrong or what?

#include <avr/io.h>
float val;
float voltage;
int led = 3;
#define solar 2 

void setup(){
 Serial.begin(9600);
 pinMode (2, INPUT);
 pinMode (0b00000001, INPUT);
 pinMode (led, OUTPUT);

}

void loop() {  
  ADMUX |= B10000000;   // internal 1.1V refference
  //We read A1 (MUX0)
  ADMUX = 0b00000001;   //PA1   
  // Start AD conversion
  ADCSRA |= (1<<ADSC);
  // Detect end-of-conversion
  while (bit_is_set(ADCSRA,ADSC));
 val = ADCL | (ADCH << 8);
 //long result = ADCL | (ADCH << 8);
  //val = result;
  int sensorVoltage = analogRead (2);
  val = (val * 5.7);
  val = (val/1024);
  val = (val*sensorVoltage);  //Multiply by the inverse of the divider
  Serial.println("val:     ");
  Serial.println(val);
  if (voltage<=2000){ //3V
    digitalWrite (led, HIGH);
  }
  else if (voltage>=2000){  //216
    digitalWrite (led, LOW);
  }
}

This is the code i am currently working on.

When you say "Arduino" do you mean an UNO

Yes, arduino UNO

If you have Vref set to 1.1V then the input voltage to he ADC can't be any higher than 1.1V.

ok, so do i need to add voltage divider at sensor end or what? but i am confused my multimeter is showing above 3.7v for sensor.

I want it to act like if voltage is above 3.5V then do something else do something else but the range is not same as or let's say even close to that, i believe. for reference i have added an led so, if the voltage reads equal to == or below 2000 read high led and if the reading goes above the range then turn it OFF.

if the value can't be higher than 1.1v then how come the sensor reads around 6v as 2500 (serial monitor reading) but the moment i shift the codes to attiny, systems does not work as needed.

Can you please show a wiring diagram of how you have things connected together and post a data sheet of the sensor

Please check the drawing and let me know if it is understandable for you. As you may see the sensor is directly connected to pin without any resistor.

NOTE: i could not find solar panel in the software so instead i used LDR.

https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny24A-44A-84A-DataSheet-DS40002269A.pdf

This is my IC datasheet.

Are you trying to use a solar panel as a light sensor?

no basically it will be charging my battery. I know i have not made connection of solar to battery but that's a later stage. Currently my focus is on this sensor, it should read specific value and act accordingly

There is no sensor shown in your schematic

oh sorry! i am calling solar panel as sensor (the one connected to PA2/ pin11) of attiny :smile:

First, you will damage your 44 if you hook it up that way, so disconnect everything and hope that the 44 is OK.
Is it your intent to read the voltage of the solar panel?

Okay, i have disconnected everything, the IC is not damaged.
Yes, i intend to read the voltage.
May, i know what can damage my IC.

Give me time to draw a little schemaic for you