Arduino SD card reader + audio output (sounds like crap)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello a few weeks ago I bought one of these SD card readers micro SD card readers for something rather unrelated to what this video is going to be about so I just checked that it worked and then I put it aside and kind of got distracted with other things but then today I received a couple of other things in the mail which made me think of using this SD card again and they are I can just get them in here this is a laser diode I think it is and it just sort of turns a laser on and off it's a laser pointer basically and this over here is a receiver and you can point the laser at that and when it detects that the laser is pointing at it it'll give you a high signal on here for your Arduino to read very very simple to use and I was very surprised that the effectiveness of this I managed to transfer data at 115 kilobits per second from about 20 meters away at least I haven't checked further than 20 meters but I know that it at least it works 20 meters away and the data that I was transmitting was just a bunch of numbers so I just had a bite and I was counting it up and then letting it overflow and just sending that sort of rather meaningless data but there was enough to see that it worked but I thought it might be interesting to try that data connection with some more meaningful content so that gave me the idea to try something else that I've been sort of thinking I'd give a give a go have a go at recently and that is trying to get some audio to play from an Arduino so that's where the SD card comes in because we can put lots and lots of data on here and the Arduino can read it in quite quickly quick enough to play audio and yeah so that's what I'm going to do in this first video and I think I'll make another video to show how you can use these two things as a data transmission connection anyway so in this video we're going to look at the ISTE card in the audio so what do we got to have a look at here I have some pages oh yeah this is the page or this is where I got it from been good pretty cheap you get three of them for three dollars 53:48 so yet not not much of a cost outlay there and they're also very very easy to use I actually when I when I got it I thought I might make a video about using it but it turns out it's just so easy it's not really worth making the video on so the library that I'm using is the official I guess you could say Arduino supplied I think it was supplied with the Arduino idea I'm not sure because I've got so many libraries from other places these days that I can't even remember where they all came from but this is the one these are the examples that I have and I'm gonna look at card info and dump file in a minute so that's where they come from if you don't have them you can probably find them on Arduino is main website and those pages for the audio stuff so let's let's just open up my do we know IDE and I'll yeah like I said whoops I think this is included with the Arduino IDE at least on my examples menu here put it on screen so you can see it and the examples menu if I go down the bottom you probably can't see it but there's SD and then in that sub menu I'm going to select card info so that'll bring us up this card info sketch and I'll just put there there and I will check that we've got this in the right place do we know who no yep so what this is going to do is it's just going to show us a little bit of information about the card that we have in there I think yes we have one in okay so just upload that and we'll when it uploads we'll see what the serial monitor says now I noticed that there's a couple of problems right off the bat in this library which was a little bit surprised about actually because it's at least I think it's the official one that that's supplied with the IDE but the problem that we're having here is the connections we should look at the connections first should we let's do that that doesn't take long if you are familiar with SPI you should should be familiar with what we're looking at here we just saved ground and VCC and then we have the four SPI pens very simple stuff and I'm using ground and VCC there this is just a regular I dunno three that just sort of different color and different maker and stuff and then over here I'm using pins 10 11 12 and 13 for the standard hardware SPI so that's the connections and the problem let me go back to here the problem is that the chip select pin is not matching and the strange thing is that if you look in the source code here it says chip select equals 4 and then somewhere down here we're using 10 so I'm not sure what's going on here but I just set this to 10 because that's what I'm using and if we try that again we should see some more useful information so this is just a dump showing you what you know type of volume you're looking at in the size and everything so it's our four are supposed to be four gigabytes microSD card and has these files on here which these ones here actually invisible when you're looking at it through most file explorers so it's an interesting that it shows them up on here and then we have this thing here fade mono this is my audio file that I have prepared earlier and that's about a three point seven gigabytes megabytes sorry and then we have a file called test txt and then another file here notice that they have they have this tilde and the name it seems that you can only use the fat32 file system with this stuff and that means that all your file names are going to be the standard eight characters and then three character extensions so you can't do any of those long long filenames that we have so commonly now but it used to be that you couldn't do that on old DOS operating systems so I think you're gonna have to call your file names by eight characters and then the three character extension otherwise okay so I'm not sure if it might just rename it itself with this tilde thing in there to make it shorter anyway so we have this file here and I'm just going to look at this test txt file to start with and we'll just dump the contents of that file to the serial monitor using the other sketch that is in the examples so now I'm going to go down to SD and I'm going to check going to select dump file it gives me this one and again I have to change your chip select whichever chip selected I'm using which is ten connections are all the same and now it's a little bit of a look at the source code here because I never really bothered to look into the details of this too much it's it's pretty simple you have a begin it's almost like the serial connection really you have a begin and then you have an open to specify which file you want I think you can go with uppercase or lowercase which is another side effect of having this fat32 or the old-style namings it doesn't matter there's no case sensitivity in the filenames so I'll just try that with a lower case for my file name and then once you have a file opened you do a very similar thing to what you do with software serial and that kind of connections where you just check if there's something available and then call read on it to get the next byte from that file and after a while this available function will return false and then you can exit the loop so that's that's what this loop is doing here and then I suppose you should close the file because it says only one file can be open at a time so it's just something to keep in mind if you're doing this doing more complex stuff and just to be pedantic the area should say that all right so let's see what happens when we upload that okay this is a text file that that is the text contents that I put into that test txt file so we can see that the dump file is working correctly it's finding the file on the micro SD card and it's just reading out each byte in the file and dumping them to the serial monitor for us to look at now in the case of my music file I don't want to dump it to the serial monitor look at I want to send it to some speakers and the way you can do that we end up with a pretty horrendous sounding music file because we have to reduce the bitrate and the quality of it down so much but let's have a look at how I've managed to do that and one page that was really really helpful when I was doing this was this page here on Instructables comm if you search for Arduino an audio output you'll find this pretty quickly and this is where I discovered a thing called a resistor letter or an hour-to-hour converter or digital it digital to analog converter and the reason it's called a letter it's probably quite obvious from the way it looks there but this lets us take an 8 byte number so you can see there's 8 wires so if you imagine that each one of these wires is representing one bit of an 8 bit number so that's a number from 0 to 255 and valued and they're being connected into these consecutive pins on the Arduino and this just happens to align with a certain port and I'll have a look at what ports are in a minute too just in case people are not where what they have they're quite quite handy for doing writing of digital pins very quickly so just imagine that each one of these pins on here is one bit of an 8 8 bit number the way these resistors work I don't totally understand this but if we look at the know which one should we look at let's look at this one so you can do this with as many bits as you need but we're only looking at 8 in this case so the reason it's called an hour-to-hour converter is because you use two values of resistance and there'll be whatever r is and then there'll be twice as much as r and the way they laid out somehow or other causes this is where I don't really follow too closely how it works but basically you get an analog value out here which represents the value of what these bits would be in a in a digital number so you'd have the most significant bit here I think and in the least significant but they're closest to ground I think that's how it works so anyway so that are on the two-hour the two-hour should be twice as high resistances that are and in this particular case the person making this instructable has recommended 10k and 20k resistors for this ladder I don't have enough 10k resistors around anymore to do this because I've used them all as pull-up or pulldown resistors for other projects but I managed to find some 7.5 k and 15 K resistors to do this with that I had enough of so you need quite a few of them you need so you need nine I think he'd nine of the 2r and it needs seven of the r that'd be right yeah alright so yes so you arrange them in this layout and at one end you're gonna have your V out so this in my case this is gonna go to some speakers so just to look at what's going on with the port's that I was talking about before fins digital pin 0 through 7 that's 8 pins they all happen to be in one port and you can probably read through the whole of this page here just search for Arduino ports or something like that port manipulation and you can see a little bit more info about how this works but basically a port is a set of pins and there's a port called D which Mets to pin 0 through to 7 and this lets you do stuff like sitting all of the pins or unsetting all of the pins of that port in one go just by writing a single number to that port so there's probably an example down here somewhere so here's an example here so you could put in your Arduino program port D and this is a special variable you can set that to be equal to well you can just set it to any any number up to 255 but what they're doing here to explain this a little bit clearer x' they're using the binary format for this number so this is pins 765 and 3 being set and all the other pins will be unset and this is very quick - it's a very quick operation it's much quicker than doing digital right to set pins individually so that's what I'm going to do in my program to set the value of each audio sample as it's red so we're going to read an audio sample which is going to be an 8 bit number from the file on the micro sd card and then we're just going to sit it directly on to port D which is going to be these 8 pins and then it's immediately going to take effect so the nice thing about this resistor letter thing is that it's perfect from being quite easy to make it's very very fast to to act so there's no there's no complicated logic or microcontrollers working it's just a plain old you know and log sort of setup so well shall we look at now I suppose oh yeah you'll need to prepare your file so that it's an 8 bit mono preferably mono audio file and in my case I also wanted to make it a very low sample rate so that I didn't exceed the value or the data transmission rate of 115 kilobits per second which is as much as I've tried with the these laser transmitter receiver things so to show a little bit of the reasoning behind what comes next 115 kilobits per second is that many bits per second so if we divide that by 8 we find that we can transmit 14,000 bytes per second and if one audio sample is one byte that means we can have a sample rate of fourteen thousand four hundred samples per second so that's fourteen thousand four hundred Hertz audio hopefully this is making sense but we'll have a look at it in audacity which shows us a graphical view which might might make it a little bit easier to understand but the reason I'm choosing fourteen point four Hertz as my audio sample rate is because of what I just said I don't want to exceed 115 kilobits per second so where is my okay so this is my file that I'm going to use or I'm going to convert to put onto the SD card and where's my okay so should be there yep so this file is a it's a bit like an mp3 it's a compressed audio file at the moment and this unfortunately is a copyright free so I should shouldn't have any problems of copyright violations or anything anyway that's that's the song there and I or destiny doesn't actually read that song or read that file or it doesn't read mp3s as well so on Linux what I do to change it to a wave which I need to do for the next step is I just use if if MPEG and so I convert it to a wave like that so this is much larger you can see it's uncompressed now but it's exactly the same and if we do it big if you if you do if if in big - I and then the file name without any destination it will tell you what format and video format and stuff that file is made from so we can see we've ended up with a forty-four kilohertz stereo signed 16-bit file and this is 1411 kilobits per second which is way too too much for us to be sending through the laser transmission thing so the way I have been converting these once I figured out how to do it is using the what's this thing called sound exchange yeah this is a this is a great little utility so Windows users obviously I don't think you'll be able I don't know there might be a win32 port of this or not I don't know so I do fade wave as the input and I need to change it to one channel so that's mono and I want the rate to be fourteen and I want the bits to be eight bits for my output file and then output file I'm gonna call fade mono and you can just do that if you just do that it'll take a thinking probably the left channel of that audio incoming audio and I discovered something than by googling around that if you do remix - like that oops is this on the screen hopefully that was on the screen but if it wasn't it should be now if you do remix - I think that's going to remix both the left and right channels into the mono channel that results so now we have a much much smaller file so this is the fade mono that we saw just before that I already have on the SD card and this is 3.7 Meg and doesn't sound as good although I guess when you're watching a YouTube video might sound about the same actually but not to make excuses or anything but the audio that we get out of our Arduino is really quite terrible but just keep in mind that we're not actually playing this high-quality file to begin with so so yeah it's now if we check that a mono just to see what kind of format we got to double check so now we can see we have 14,000 14,000 Hertz mono unsigned 8-bit values so if you are using Windows you may not be able to do that another way you can do it is to use audacity which is a free okay so you still need to convert it from mp3 or whatever you have to a wave I'm not sure you can do that on Windows there's probably plenty of utilities that can do that for you but once you've got your high quality WAV file you can open it with audacity and then you can do tracks stereo track to mono and then you can do project rate to oh yeah I think you can type in here so I'm going to type in more and that should work like that and if it doesn't work you can also do resample or something in resample at 14400 as well so i'll just do that to show you something else that's quite useful to illustrate what's going on here so Rhys at 14 4 and now when we play it like I say you may not be able to tell the difference in the YouTube video but it's a fair bit lower quality audio so now let's look at the beginning of this file zoom in there zoom right in so here we can see each of those notes being played if we deliver iodine to the beginning again where is it oh so we can see some values all right so when we zoom right in we're actually showing each one of these audio samples as a dot you can see them sort of going up and down in a wave sort of a fashion strangely enough and that's how the audio is made up now I want to look at about here okay doesn't really show it too well but so what we have here is if you can imagine each one of these dots is a number between 0 and 255 because we're using 8-bit values right so this right in the middle it's going to be about 127 128 or something isn't it and as it fluctuates up and down we're going to see numbers closer going to 0 or up to 255 and if we look at this file that we got as a result of converting to 8-bit audio and we look at that with a hex editor like this we'll see a little bit of header information at the beginning this is just a standard wave header and when we play the audio later we'll actually be able to hear what that sounds like when we play it it just sounds like a kind of noise at the beginning but after that let's go down here a little bit now we see a whole lot of numbers 8 0 8 0 8 0 so each one of these fights and here is one of those samples that we saw in audacity which is gone there so each one of these dots is one of these bytes and you can see 8 0 is exactly if it's unsigned 8-bit is 128 which is kind of what we expect it's trippy right in the middle and goes on for quite awhile like that 7f is 127 so basically hmm nothing's really changing and that's what we see here on on the visual view and bit in the background and if we scroll down a bit then we start to get into some actual music which is this kind of stuff here so when the values are fluctuating more than just one by one number then that's what it looks like there so how do we know program it's just going to read out one of these bytes and send it to the port and it's just going to do that over and over and over until it gets to the end of the file all right so you probably want to hear what this sounds like right oh so - to save this once you done once you've set the project rate so the project rate at the bottom and do this where's that stereo track the mono stuff you can export export audio and then you'll need to select other uncompressed files and it's already selected okay so other uncompressed files and then an options you will need to say unsigned 8-bit PCM so there should be an option for that the the header type doesn't really matter because it's only gonna play a little bit of noise at the beginning of the file anyway anyway so that's how you would export using audacity instead of the Sox let's just leave it open for now alright so we want to hear what this sounds like right so over here I have my resistor letter doesn't look quite as neat and tidy as the one in the instructables photos but it should be functionally fairly equivalent I'm not sure how much the lengths of wire here is going to fit affect the quality of the sound but yeah that sounds pretty bad so it could be that the the wire is affecting it a fair bit all right I've connected everything up now the Pens to 0 through 7 just go straight on today each of those resistors and then as a ground that goes over there to the ground there and then I'm left with another I guess you could call it ground and positive but it doesn't really matter with audio either way because all it's doing is moving the coils in the speaker one way or the other so the only difference in switching them around would be is would be having will give it effect as if you're standing on the other side of the speaker so it's not it doesn't really matter so I'm going to connect these up to the speakers from my computer amplifier desktop computer speakers but just remember that we haven't actually changed the program to make this work so if I come back over to this dumpfile program which was doing what was this doing oh yes it's dumping the text file okay so we don't want to dump the text file we want to dump the little fade mono fade is just the name of that song by the way so no it's not actually fading sound or anything so this time we're going to try opening the wav file and instead of writing it to the serial monitor we're going to do that port D thing so that we just sit port D which is going to be the 8 bits along here is going to be given the value of whatever byte we read from the SD cards file right oops so it's upload that so I just open the serial monitor there's nothing to look at hopefully okay so it should be running now and with these so if these plugs here the base terminal the one at the base is going to be the common terminal or your ground you might call it I suppose and then the other two that outside are the left and right channels of the audio so you will need to put one of these onto the base so that's one's gonna go over there and the other connections going to go to one of the other two so just see if I can hold these on like this yeah and hopefully rule doesn't that sound great so I can do lift yeah that's lift and right it's going too fast isn't it all right so I pretty soon I got sick of holding it on like that but I just wanted to show you that that's how you can do that so I made a little plug so I don't have to keep holding it so I just cut one of these connections to have bring out the wires from inside and then the one that has no color on it I'm assuming is the common so that's what I used to do this so I'll just plug this in here in place of that one and then I won't have to keep holding it because it is very awkward to hold all right so it's too fast and that's kind of a good sign because it means that Jesus shut up that's kind of a good sign because it means that we can read data from the SD card fast enough to play the audio if it wasn't fast enough we might have to do some other method of reading and there is the first way we can do this so what we're doing at the moment is we're checking if there's a byte available and we're doing data file read that will be one byte so every bite we read we're going through this loop but if you do need to read something faster I found it as a function in this SD card library you could do read and then you'll give it a buffer so you need a buffer of bytes and then however many bytes you want it to read into that buffer and that's much faster but unfortunately we don't need to do that but what we do need to do to make that music sound a little bit more sensible is to slow it down and I tried doing a few things like this and this is about let's try show can you that it's it's super super slow so we could try this microseconds strike one let's try ten microseconds was kind of reasonable it's still too fast that's better I found that you couldn't really get grass orrible you I couldn't really get a very close match to the original speed by using delay microseconds somewhere around 18 19 or something was a good match so another method I tried was to do something like this so instead of using delay microseconds so the problem with delay microseconds is that it was like 18 was too fast and 19 was too slow or something there wasn't enough granularity in there in the numbers so one thing I tried was this you just go around and around and loop doing and know up so that the know up is one instruction cycle or one cycle and then the loop itself is going to take a few instructions to to process as well now you may be saying well why don't you just do when you just do that without the know up and I found that they didn't didn't work it just sort of gets optimized the way so you need to have a no op and therefore it - for the compiler to actually pay attention to what you're trying to do this worked out fairly reasonably and I found that the value of 68 was gave me a fairly close to the speed of the original song sounds awful it sounds worse than it did before and they're actually taking some of him taking some of these out gave a bit of sound this one in particular I'm just gonna leave that one out that's actually bitter and the the ones up here the least significant bits a lot of these really don't seem to make much difference at all so I'm not sure what's up with that see I can take all of these top three out maybe not well it's a bit of a weird thing how this works so anyway this is probably the best result I've had so far with this 68 no ops another thing you could do if you wanted to get it really precise is just do a whole bunch of knobs like that until you get to however many cycles it needs to slow down by and I tried that but it got to more than about it was over almost 300 of those it took to do it so it just became a bit silly so I decided to use a loop instead so somewhere around 68 of this loop seems to match up fairly well to the original speed so that's about all I wanted to look at in this video I will just look at one other thing though and that is I notice in the instructables page that they are putting some capacitors and stuff on here and using a proper amplifier and things I couldn't be bothered with that but I did thought I think I just try putting some caps over the oh this isn't gonna work hey I'm gonna okay sorry yeah I forgot about that for some reason we hook it up to my computer's sound system putting a capacitor over the the outputs there just makes it stop completely but when I was doing this earlier with my my headphones putting a capacitor over the output sort of rounded the sound off a little bit it wasn't it didn't make it better it just sort of made it sound like it was muffled a little bit like coming from the room next door kind of sound so I'm not really expecting to get any better sound than what we just got from this unfortunately it does sound pretty terrible but this will be something interesting to try sending this audio data from here on one I do ain't Arduino using the laser transmitter receiver up onto another one and see if we can transmit some audio just using pure light so we'll do that next time see ya
Info
Channel: iforce2d
Views: 166,258
Rating: undefined out of 5
Keywords: arduino audio, wav conversion, arduino laser pointer
Id: b1CdC5gzTxY
Channel Id: undefined
Length: 39min 35sec (2375 seconds)
Published: Sat Apr 09 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.