Hi, I have a orangutan mega8, and am tryin got configure a humidity sensor which is described by manufacturer as a 2-terminal variable resistance humidity sensor. just a little box to two metal contact prongs sticking out the bottom.
I have one of those going into the signal i/o and one going to ground. My program flashes an led based on the value of the humidity sensor.
But i cant get it to change and therefore wonder if the sensor is actually sensing? I held a boiling kettle under it and nothing… I am also wondering whether my code is refreshing the value once it has been set - does my infinte for loop work? It is on ADC6.
Here is the code
// F_CPU tells util/delay.h our clock frequency
#define F_CPU 8000000UL // Orangutan frequency (8MHz)
//#define F_CPU 20000000UL // Baby Orangutan frequency (20MHz)
#include <avr/io.h>
#include <util/delay.h>
void delayms( uint16_t millis ) {
while ( millis ) {
_delay_ms( 1 );
millis--;
}
}
void analog_init(void)
{
// The Orangutans have an external reference voltage on the AREF
// pin tied to our +5V regulated Vcc. We want to set our ADC to
// use this as our reference. The ADMUX register needs REFS0
// set to 1, and REFS1 set to zero for this mode.
ADMUX = (1 << REFS0);
// The ADC Control and Status Register A sets up the rest of
// what we need. Three bits ADPS0, 1, and 2, set the prescale
// for how fast our conversions are done. In this example
// we use CPU/64, or mode 6:
ADCSRA = (6 << ADPS0);
// Conversions take 13 ADC cycles to complete the sample and hold.
// Dividing our CPU by 64 gives us the following samples/sec for
// our devices:
//
// Orangutan 9615
// Baby-O 24038
// X2 24038
// Finally, we enable the ADC subsystem:
ADCSRA |= (1 << ADEN);
}
// Read out the specified analog channel to 10 bits
unsigned int analog10(unsigned char channel)
{
// Begin by setting up the MUX so it knows which channel
// to connect to the ADC:
// Clear the channel selection (low 5 bits in ADMUX)
ADMUX &= ~0x1F;
// Select the specified channel
ADMUX |= channel;
// Now we initiate a conversion by telling the ADC's
// control register to start conversion (ADSC):
// ADC stert conversion
ADCSRA |= (1 << ADSC);
// We wait for conversion to complete by watching the
// ADSC bit on the ADCSRA register. When it goes away,
// the conversion is done:
while (bit_is_set(ADCSRA, ADSC));
// Since we're reading out ten bits, we have to read
// the results out of two different registers: ADCL for
// the low byte, and ADCH for the high byte. Caution
// here: We have to read ADCL before we read ADCH.
// Since they're both just bytes, we have to shift the
// high byte over by 8-bits and or the two together
// to make the full 10-bit value:
return (ADCL | ADCH << 8); // read ADC (full 10 bits);
}
// And now for the maximum in cheesiness: How do you do an
// 8-bit conversion if you're in 10-bit mode? Simple! Do a
// 10-bit conversion and ditch the two least significant bits.
// Laugh all you want, that's how AVRLIB does it, too.
// Read out the specified analog channel to 8 bits
unsigned int analog8(unsigned char channel)
{
return(analog10(channel) >> 2);
}
// That's it for the ADC stuff. Remember, there are lots of other
// ways the ADC subsystem can be used. This is just ONE way. If it
// doesn't meet your needs, read the data sheet and experiment.
// And now for our main routine:
int main( void )
{
while(1)
{
// A variable to store our converted analog value in:
int pot;
// Initialize the ADC:
analog_init();
DDRB |= (1 << PB0); // set buzzer pin PB0 to output
DDRC |= (1<< PC5);// led breadboard
// DDRB &= ~(1 << PB3); // make PB3 (button SW3 pin) an input
DDRD |= (1<<PD1); //Led output
// The endless loop
for(;;)
{
// Store the value of our analog input:
pot = analog10(6);
if (pot > 50)
{
//PORTB &= ~( 1 << PB0 ); // buzzer off
PORTD |= (1 << PD1);//LED ON
//PORTC &=~ (1<< PC5);// turn off breadboard
delayms( pot/5 ); // delay 50 ms
//PORTB |= (1 << PB0); //BUZZER ON
//PORTC |= (1 << PC5);//turn on bread boar
PORTD &= ~(1 << PD1); // LED OFF
delayms( pot/5 ); // delay 50 ms
}
else
PORTB &= ~(1 << PB0);
PORTD &= ~(1 << PD1);
}
}
return 1;
}
Thanks guys. Any ideas?
Alex