reading audio using mcp3008 - Raspberry Pi Forums
hello everyone,
trying read audio data microphone through mcp3008 doesn't seem work. have tested code potentiometer , working fine. when play 1 khz tone , not seem capture data microphone.
new raspberry pi, please help.
code using posted below:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringpi.h>
#include <wiringpispi.h>
#include <wiringserial.h>
#define cs_mcp3202 8 // bcm_gpio8 //pi24
#define spi_channel 0
#define spi_speed 200000 // !! start low here , if works try increase if needed on breadboard go upto 750000
#define pi 3.1416
#define spi_speed 11 // bcm_gpio8
int read_mcp3202_adc(unsigned char adcchannel)
{
unsigned char buff[3];
int adcvalue = 0;
wiringpispisetup(0, spi_speed);
buff[0] = 0x01;
buff[1]=0x00 | adcchannel;
buff[2] = 0x00;
digitalwrite(cs_mcp3202, 0); // low : cs active
usleep(0.10);
wiringpispidatarw(spi_channel, buff, 3);
buff[1] = 0x0f & buff[1];
adcvalue = ( buff[1] << 8) | buff[2];
digitalwrite(cs_mcp3202, 1); // high : cs inactive
usleep(0.27);
return adcvalue;
}
int main (void)
{
float x[360];
unsigned char adc1channel = 0x80;;
int adc1value = 0;
int i;
if(wiringpisetup() == -1)
{
fprintf (stdout, "unable start wiringpi: %s\n", strerror(errno));
return 1 ;
}
if(wiringpispisetup(spi_channel, spi_speed) == -1)
{
fprintf (stdout, "wiringpispisetup failed: %s\n", strerror(errno));
return 1 ;
}
pinmode(cs_mcp3202, output);
for(i=0 ; i<360;i++)
{
// w= (i*pi/180);
// t = (2*pi)/w;
system("clear");
//printf("\n\nmcp3202 channel output.\n\n");
adc1value = read_mcp3202_adc(adc1channel);
//printf("adc0 value = %04u", adc1value);
//printf("\tvoltage = %.3f\n", ((3.3/1024) * adc1value));
x= ((3.3/1024)*adc1value);
usleep(1);
}
for(i=1;i<360;i++)
{
printf("%f \n ",x);
}
return 0;
}
code compiled , csv file made analyze recorded data. since csv file not being attatched, going post data it in nxt post.
trying read audio data microphone through mcp3008 doesn't seem work. have tested code potentiometer , working fine. when play 1 khz tone , not seem capture data microphone.
new raspberry pi, please help.
code using posted below:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringpi.h>
#include <wiringpispi.h>
#include <wiringserial.h>
#define cs_mcp3202 8 // bcm_gpio8 //pi24
#define spi_channel 0
#define spi_speed 200000 // !! start low here , if works try increase if needed on breadboard go upto 750000
#define pi 3.1416
#define spi_speed 11 // bcm_gpio8
int read_mcp3202_adc(unsigned char adcchannel)
{
unsigned char buff[3];
int adcvalue = 0;
wiringpispisetup(0, spi_speed);
buff[0] = 0x01;
buff[1]=0x00 | adcchannel;
buff[2] = 0x00;
digitalwrite(cs_mcp3202, 0); // low : cs active
usleep(0.10);
wiringpispidatarw(spi_channel, buff, 3);
buff[1] = 0x0f & buff[1];
adcvalue = ( buff[1] << 8) | buff[2];
digitalwrite(cs_mcp3202, 1); // high : cs inactive
usleep(0.27);
return adcvalue;
}
int main (void)
{
float x[360];
unsigned char adc1channel = 0x80;;
int adc1value = 0;
int i;
if(wiringpisetup() == -1)
{
fprintf (stdout, "unable start wiringpi: %s\n", strerror(errno));
return 1 ;
}
if(wiringpispisetup(spi_channel, spi_speed) == -1)
{
fprintf (stdout, "wiringpispisetup failed: %s\n", strerror(errno));
return 1 ;
}
pinmode(cs_mcp3202, output);
for(i=0 ; i<360;i++)
{
// w= (i*pi/180);
// t = (2*pi)/w;
system("clear");
//printf("\n\nmcp3202 channel output.\n\n");
adc1value = read_mcp3202_adc(adc1channel);
//printf("adc0 value = %04u", adc1value);
//printf("\tvoltage = %.3f\n", ((3.3/1024) * adc1value));
x= ((3.3/1024)*adc1value);
usleep(1);
}
for(i=1;i<360;i++)
{
printf("%f \n ",x);
}
return 0;
}
code compiled , csv file made analyze recorded data. since csv file not being attatched, going post data it in nxt post.
3.296777
3.296777
1.65
0
0
0
0
0
3.296777
3.296777
3.296777
3.14209
2.056055
2.8875
2.732812
2.8875
0
0
0
0
0.425391
3.009961
2.939063
2.475
2.674805
1.65
0
0
0
0
0
3.296777
3.296777
2.884277
3.296777
3.296777
3.296777
2.8875
2.8875
0
0
0
0
0
0
0
0
3.296777
2.526562
3.296777
1.959375
3.296777
3.296777
0
0
0
0
3.296777
3.051856
3.296777
3.296777
2.475
2.494336
3.296777
1.65
0
3.087305
3.296777
3.296777
0
0
0
0
0
0.20625
3.132422
3.171094
3.080859
3.258106
2.26875
2.98418
2.8875
0
0
0
0
0
3.296777
0
0
0
0
3.296777
3.296777
3.296777
2.475
3.296777
3.296777
3.296777
2.475
0
0
0
0
0
2.72959
3.296777
3.296777
2.861719
2.475
3.296777
3.080859
0
0
0
0
0
3.296777
3.296777
3.296777
2.475
3.15498
2.165625
2.939063
2.8875
1.65
0
0
0
3.296777
3.296777
0
3.013184
3.296777
3.296777
2.597461
0
2.410547
0
0
0
0
0
3.296777
2.368652
2.758594
3.296777
2.884277
2.690918
3.296777
1.695117
2.8875
0.825
0
0
0
0
0.825
3.296777
2.394433
2.475
3.296777
2.497559
2.262305
2.188184
0
0
0
0
0
2.484668
3.296777
3.296777
3.296777
3.296777
3.296777
2.8875
0
0
0
0
0
0.051562
3.296777
0
0
0
2.964844
3.296777
3.296777
3.296777
2.32998
2.081836
2.552344
3.09375
0
0
0
0
0
3.296777
3.296777
2.68125
2.381543
2.932617
3.296777
2.771484
0
0
2.320312
3.071191
3.296777
2.8875
0
0
0
0
3.296777
2.719922
3.296777
2.010937
2.732812
2.8875
0
0
0
0
0
0
0
2.832715
0
0
0
3.296777
3.296777
2.597461
2.475
3.296777
3.296777
3.296777
0
0
0
0
0
0
3.296777
3.296777
3.296777
2.475
3.293555
2.475
1.853027
2.475
0
0
0
0
0
3.296777
2.668359
2.555566
3.296777
1.65
3.296777
3.296777
0
0
0
0
0
3.296777
2.32998
0
0
0
3.296777
2.8875
0
0
0
0
0
3.296777
3.296777
3.296777
3.296777
3.296777
3.296777
0
0
0
0
0
3.296777
3.296777
2.552344
2.587793
2.8875
3.296777
3.296777
2.8875
0
0
0
0
0
3.296777
3.103418
3.296777
2.475
3.061523
3.296777
0
0
0
0
0
3.296777
2.771484
3.296777
2.803711
3.296777
2.371875
3.296777
2.600684
3.206543
2.910058
3.09375
3.296777
0
0
0
0
0
0
3.296777
3.296777
3.042187
2.926172
2.964844
2.655469
2.8875
2.8875
0
3.296777
3.296777
0
0
0
0
0
0
2.114063
3.296777
3.248437
3.296777
3.296777
1.65
0
0
0
0
0
3.296777
3.296777
3.296777
3.14209
2.056055
2.8875
2.732812
2.8875
0
0
0
0
0.425391
3.009961
2.939063
2.475
2.674805
1.65
0
0
0
0
0
3.296777
3.296777
2.884277
3.296777
3.296777
3.296777
2.8875
2.8875
0
0
0
0
0
0
0
0
3.296777
2.526562
3.296777
1.959375
3.296777
3.296777
0
0
0
0
3.296777
3.051856
3.296777
3.296777
2.475
2.494336
3.296777
1.65
0
3.087305
3.296777
3.296777
0
0
0
0
0
0.20625
3.132422
3.171094
3.080859
3.258106
2.26875
2.98418
2.8875
0
0
0
0
0
3.296777
0
0
0
0
3.296777
3.296777
3.296777
2.475
3.296777
3.296777
3.296777
2.475
0
0
0
0
0
2.72959
3.296777
3.296777
2.861719
2.475
3.296777
3.080859
0
0
0
0
0
3.296777
3.296777
3.296777
2.475
3.15498
2.165625
2.939063
2.8875
1.65
0
0
0
3.296777
3.296777
0
3.013184
3.296777
3.296777
2.597461
0
2.410547
0
0
0
0
0
3.296777
2.368652
2.758594
3.296777
2.884277
2.690918
3.296777
1.695117
2.8875
0.825
0
0
0
0
0.825
3.296777
2.394433
2.475
3.296777
2.497559
2.262305
2.188184
0
0
0
0
0
2.484668
3.296777
3.296777
3.296777
3.296777
3.296777
2.8875
0
0
0
0
0
0.051562
3.296777
0
0
0
2.964844
3.296777
3.296777
3.296777
2.32998
2.081836
2.552344
3.09375
0
0
0
0
0
3.296777
3.296777
2.68125
2.381543
2.932617
3.296777
2.771484
0
0
2.320312
3.071191
3.296777
2.8875
0
0
0
0
3.296777
2.719922
3.296777
2.010937
2.732812
2.8875
0
0
0
0
0
0
0
2.832715
0
0
0
3.296777
3.296777
2.597461
2.475
3.296777
3.296777
3.296777
0
0
0
0
0
0
3.296777
3.296777
3.296777
2.475
3.293555
2.475
1.853027
2.475
0
0
0
0
0
3.296777
2.668359
2.555566
3.296777
1.65
3.296777
3.296777
0
0
0
0
0
3.296777
2.32998
0
0
0
3.296777
2.8875
0
0
0
0
0
3.296777
3.296777
3.296777
3.296777
3.296777
3.296777
0
0
0
0
0
3.296777
3.296777
2.552344
2.587793
2.8875
3.296777
3.296777
2.8875
0
0
0
0
0
3.296777
3.103418
3.296777
2.475
3.061523
3.296777
0
0
0
0
0
3.296777
2.771484
3.296777
2.803711
3.296777
2.371875
3.296777
2.600684
3.206543
2.910058
3.09375
3.296777
0
0
0
0
0
0
3.296777
3.296777
3.042187
2.926172
2.964844
2.655469
2.8875
2.8875
0
3.296777
3.296777
0
0
0
0
0
0
2.114063
3.296777
3.248437
3.296777
raspberrypi
Comments
Post a Comment