POSTS
Week 11: Networking
This week I sent data from my load cell and pot to an SD card and then eventually will print out results on an OLED screen.
I used a microSD card adapter to send data through SPI and then parsed and did calculations in python and passed back info in readable form to the OLED screen.
Here’s the schematic for how the microSD adapter hooks up to my microcontroller.
Communication does not work because you can’t trigger the pins up and down fast enough due to timing, most likely because of extra caps and resistors.
Here’s some of the garbage data I was getting.
Before this, we were attempting to start switching up and down but the voltage was too low. When we tried to wait until the voltage got higher by adding a 1ms delay, we were at the right voltage but couldn’t switch back down because the capacitance was too high to switch the pin back down. (too much cap storage)
Solution is to either check datasheet, figure out how caps and resistors are working to fix timing, but instead I just chose to mill a new board with no caps and resistors.
I started creating the board Neil designed:
I created the schematic and then milled it. I soldered the parts on, including the SD card holder- pins were tiny on that component but I was able to use an iron to solder all the components on.
I tried using this micro SD card but ran into a ton of problems including getting garbage data, inconsistent results and different results when plugging the microSD in and out. I was getting more data than last time when I was using my old board but still not great results.
I got excited when I saw this because it resembled Neil’s output but still was missing the actual text. Note that I created a file called ‘Alice’s Adventures in Wonderland’, and copy pasted Neil’s text file into it:
ALICE'S ADVENTURES IN WONDERLAND
Lewis Carroll
THE MILLENNIUM FULCRUM EDITION 3.0
CHAPTER I. Down the Rabbit-Hole
Alice was beginning to get very tired of sitting by her sister on the
bank, and of having nothing to do: once or twice she had peeped into the
book her sister was reading, but it had no pictures or conversations in
it, 'and what is the use of a book,' thought Alice 'without pictures or
conversations?'
So she was considering in her own mind (as well as she could, for the
hot day made her feel very sleepy and stupid), whether the pleasure
of making a daisy-chain would be worth the trouble of getting up and
picking the daisies, when suddenly a White Rabbit with pink eyes ran
close by her.
...
Also, I had to make sure the microSD was formatted correctly (FAT32). I was using some microSD someone let me borrow and it had 5 different partitions. I went through, deleted the partitions, and added a new one formatted to be FAT32. Here are the steps and a link: (make sure you are working on the correct disk!!!! Don’t delete your entire system!!!!! Plug the USB in and out to check that it’s the right one (in my case sdb))
sudo parted /dev/sdb
(parted) mklabel msdos
(parted) mkpart primary fat32 1MiB 100%
(parted) set 1 boot on
(parted) quit
sudo mkfs.vfat /dev/sdb1
https://www.cio.com/article/3176034/how-to-format-an-sd-card-in-linux.html
Here’s a separate link for deleting partitions and creating a new one although the first link I posted worked better for me.
https://ragnyll.gitlab.io/2018/05/22/format-a-sd-card-to-fat-32linux.html
I went through a bunch of code examples online to see if I was missing an initialization of the SD or if I was reading and writing correctly. It seemed like Neil’s code was aligned with most things online and no changes I made fixed anything.
For example I tried adding more garbage data output in the SD command function but with no success:
void SD_command(uint8_t command,uint32_t argument,uint8_t CRC,uint8_t *result) {
clear(CS_port,CS_pin);
SPI_write(command);
SPI_write((argument >> 24) & 0xFF);
SPI_write((argument >> 16) & 0xFF);
SPI_write((argument >> 8) & 0xFF);
SPI_write(argument & 0xFF);
SPI_write(CRC);
result[0] = SPI_write(0xFF);
result[1] = SPI_write(0xFF);
result[2] = SPI_write(0xFF);
result[3] = SPI_write(0xFF);
result[4] = SPI_write(0xFF);
result[5] = SPI_write(0xFF);
result[6] = SPI_write(0xFF);
result[7] = SPI_write(0xFF);
//I added these three lines
// result[8] = SPI_write(0xFF);
// result[9] = SPI_write(0xFF);
// result[10] = SPI_write(0xFF);
set(CS_port,CS_pin);
}
I then decided to find another microSD, specifically a SanDisk microSCHC 16 GB and see if that worked. It did but still inconsistently.
You can see the output looks good here but it took a lot of plugging the microSD card in and out.
Here’s a useful tutorial on the FAT32 library.
https://codeandlife.com/2012/04/07/simple-fat-and-sd-tutorial-part-2/
I tried opening the serial terminal ASAP after compiling the C file. Turns out the problem was the serial terminal- when I simultaneously opened the serial terminal (or just left it open) while compiling I could see everything just fine. It was an issue of timing between the serial communication and the code running. This won’t really be an issue when actually reading and writing from the SD card because it has nothing to do with reading from the serial terminal.