Hi is anyone able to help me convert this arduino code to avr. Trying to make a sound pressure level meter using the same idea as this link https://circuitdigest.com/microcontroller-projects/arduino-sound-level-measurement just need to change the code to avr. I am using the atmega 164p.
const int MIC = 0; //the microphone amplifier output is connected to pin A0
int adc;
int dB, PdB; //the variable that will hold the value read from the microphone each time
void setup() {
Serial.begin(9600); //sets the baud rate at 9600 so we can check the values the microphone is obtaining on the Serial Monitor
pinMode(3, OUTPUT);
}
void loop(){
PdB = dB; //Store the previous of dB here
adc= analogRead(MIC); //Read the ADC value from amplifer
//Serial.println (adc);//Print ADC for initial calculation
dB = (adc+83.2073) / 11.003; //Convert ADC value to dB using Regression values
if (PdB!=dB)
Serial.println (dB);
if (dB>60)
{
digitalWrite(3, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(3, LOW);
}
//delay(100);
}
Thank you so much. Have you done a project similar to this before https://circuitdigest.com/microcontroller-projects/arduino-sound-level-measurement. Just wanted to know if this will work for the ATMEGA 164p. My teachers have said I need a rectifier after the amplfication of the circuit but Im not sure if one is present in this circuit or if I do need one. Thanks again
You can remove all the unused variables from the code.That's why warning is coming
Debasis Parida
Joined August 22, 2019 125Thursday at 12:29 PM
Use the following code format
#include <avr/io.h>
#include <util/delay.h>
void adc_init()
{
// AREF = AVcc
ADMUX = (1<<REFS0);
// ADC Enable and prescaler of 128
// 16000000/128 = 125000
ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}
// read adc value
uint16_t adc_read(uint8_t ch)
{
// select the corresponding channel 0~7
// ANDing with '7' will always keep the value
// of 'ch' between 0 and 7
ch &= 0b00000111; // AND operation with 7
ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits before ORing
// start single conversion
// write '1' to ADSC
ADCSRA |= (1<<ADSC);
// wait for conversion to complete
// ADSC becomes '0' again
// till then, run loop continuously
while(ADCSRA & (1<<ADSC));
return (ADC);
}
int main()
{
uint16_t adc;
char int_buffer[10];
DDRC = 0x01; //output pin
// initialize adc
adc_init();
while(1)
{
adc = adc_read(0); // read adc value at PA0
int dB = (adc+83.2073) / 11.003; //Convert ADC value to dB using Regression values
if (dB>60)
{
PORTC=0X01;
_delay_ms(2000); // wait for a second
PORTC=0X00;
}
}
}