Intro: ESP32 ADXL345 DATALOGGER WITH GPS_EXT RAM_EXT_RTC
For those of you out there playing with the Wemos 32 LOLIN board I thought i would start to document some findings of mine so far.
Current project is to interface to an ADXL345 accelerometer and as the photo shows above i have successfully connected it to the lolin and configured the ADXL as a shock sensor.
The registers on the ADXL have been set up as the attached pdf and the interrupt i have configured in the code to spit out the code on the serial port once a shock is detected.
I also set up the inactivity register as an interrupt and after checking which interrupt has been triggered then spit out the same data.
I am using the I2c port on the LOLIN and you will see i have a routine to spit out the registers on the ADXL so i can check the config while debugging. This function is running in a timer function using the ticker library.
This is a useful function to set up for debugging and checking the registers in case you write something in there which mysteriously causes the data to do some strange things.
I have normalised the 2's compliment data in the LOLIN and formatted it so it can be imported into excel.
Have a look at teh attached PDF with some data i have imported and i have graphed in excel showing some taps on the ADXL which are triggered by an interrupt in the trigger register using FIFO mode.
FIFO mode is a useful feature which does not tie the micro up and it stores 32 samples on a trigger.
Again look at my data dumps and you can see where we start at ground zero and the last sample is at 9.8ms or there about.
X line on the graph shows the timing in microsecs increasing left to right.
NOTE I HAVE SINCE AMENDED THE LAST GRAPH TO SHOW TIMELINE FROM MINUS 9800 microsecs.
First pop of FIFO is the earliest data the rest is older.View should be from right to left.
Note the three wires to the ADXL board. SDA/SCL and the INT from INT 1. Again if you look at the register settings and cross ref the data sheet it will make sense.
Data sample is full tilt at 3200 samples which gives 3125 useconds between samples.and a pre capture of 4 samples. Look at the pdf of the data from the device in excel and each of the graphs i have plotted showing me moving the window of capture.
I will put up some code of the config and interrupts if anyone is interested.
For I2C i am using the wire library and have written some functions around that.
Notice the little data sniffer which i have connected to SDA/SDL and using Sigrok i can decode the I2C bus in real time.
Next step is to save to SAN disk although i have already proved it works. Once that is done I will interface to the wireless and upload to a website.
I will add to this as the project expands.
For the observant ones out there you will notice a large dent in the can protecting the esp chip and a jumper on the pcb. This has been due to the resident Cocker Spaniel who inspected the mail and decided to chew the board before letting me have access to it. I think she is a non espressif fan!.
Of course as always i am always up for questions so please ask.
Step 1: SIGROK AND PULSEVIEW
Just a quick mention of pulseview and sigrok.
This is free software from the net and the little interface board with 8 logic inputs is cheap from ebay et al.
You will notice a couple of images i grabbed off the bus while the ADXL was running and it is so useful again for debugging as it has a built in decoder for I2C.
A scope is great for checking signal levels but hand decoding of I2c is tiresome at best although i have set up a loop before now and hand debugged. You have to have an appreciation of bit banging at teh port level which i have done on many pic projects but it is time consuming and prone to errors...especially at night!!
Thanks to the guys who wrote this app. Its a god send for projects in i2c,
Note the D4 line which is monitoring the interrupt line from the ADXL.
Step 2: Adding to the Shock Sensor
Ok here i have added some peripherals to the shock sensor for proof of concept.
Excuse the rats nest for now once it works i'll design the pcb that links all components and put it all into a pretty box. All the items attached except the SD card are working on i2c which is on the spi bus.
Left to attach is the GPS module which is WIP but i hope to have a solution by the end of this week.
So current project consists of:
ESP32 LOLIN board with wireless.
PCF real time clock .Keeps track of current date and time. I have rough soldered onto an old project board i have etched previously.
External flash. Holds setup data for Accelerometer amongst other things.Capacity 132k and may dump some web data on it to facilitate menus etc
SPI SD card for storing file accelerometer data and logs. 8 GB but can expand.
OLED display to show menus and a few other things.
Here is what it will [eventually] do
Monitor shocks and activity that exceed background.
Register the shocks to SD card with time and date from onboard clock.
Stamp the location from GPS to SD card if avail
Using an access point upload the data to a web server to analyse the data...this can be a mobile phone.
|Scanning for access points is an ability of the LOLIN32 as is hosting a web server for commands and then connecting as a client to the cloud webserver. You can always just unplug the sd card and upload that!
Still lots to do but its moving on.
Step 3: ADXL ADDRESSING
Now here is a funny thing.
I purchased the ADXL 345 chip as a little board coz it was cheaper than sourcing a chip on its own.....how does that work? Anyway after that i set about hooking it up to the i2c bus and found i had a clash with the eeprom with addresses which effectively start at 0x53 which translates to a write at A6 and a read at A7.
So reading the bumf it turns out that if you take the SDO/ALT ADDRESS HIGH you can force it to 1D
Turns out that my little board is hardwired to ground on pin 12 of the ADXL despite the SDO pin which looks like you can pull it high. Dont try this at home without a current limited power supply....luckily i had which caused me to take out the multimeter and check the pin to ground.[Dead short] which means you have to cut the track and jumper pin 12 to 3.3v. This works and get you out of the prom clash.Luckily they did not do the short under the chip or else i would have been snookered and had to remove the chip from the board.
The EEprom which is a spare one i had is arranged as two pages of 64k as address 0x52 and 53. Once i did this my problem was solved.
Reading the web page again it does say its set to 0x53 but its in the small print so watch out!
Step 4: Schematic PCB Prints
Here is a quick run through of the circuit and components.
This is very much my design and still WIP[Work In Progress]
The schematic was designed and then translated into the artwork for the tracks.
I have verified all the components function as expected using my rats nest approach and now am assembling onto a board that will fit into a small box[Detail to follow]
Referring to shocker.pdf.
The board consists of 8 main components
- SD CARD STORAGE
- ADXL ACCELEROMETER
- DISPLAY OLED
- 3.3 V REG
- GPS1 MODULE
- TIMER RTC
- ESP32 LOLIN CHIP WITH RF WIRELESS INTERFACE
Everything hinges on the accelerometer.
The ADXL is set up to interrupt the processor on INT1which is routed to pin 14 on the processor.
The code in the micro is set up to intercept this interrupt and set a flag which is acted on in the main routine.
I will add the code outlining registering the interrupt routine and the callback function later.
The ADXL supports interrupts on different states such as a shock or inactivity plus a few others.
You can mask out the ones that tickle your fancy based on what you are trying to do.
The ADXL is operated in FIFO mode so that it stores 32 samples to capture the shock event as XYZ [96 values]
The display ADXL RTC and EEPROM are all driven from I2C. The SD CARD is connected to the SPI I/O and the GPS module is connected to the serial ports on the LOLIN marked X12 X11.
The sequence is as follows
Constantly grab the 232 data from the GPS unit and filter.
While you have valid GPS Time update the RTC at set intervals.
On interrupt store variables to SD Card such as time/Lat_Lng/Speed/Altitude/
Store ADXL registers in raw format 2's compliment and non 2's complement.
All data is comma delimited.
You will note also that there is a reset pin which is connected to pin 13.
This pin will start the built in webserver allowing you to connect to the configurator to set up an access point which is stored in the EEPROM. On Reboot the processor will connect to the access point and access the internet to upload files from the SD Card.If there is no data and no connect point then the unit just carries out its data logging process to the sd card which you can interrogate using the program [ShockerView.exe][WIP]
On the server the data is stored in an SQL database and displayed[WIP]
The PCB Prints are attached for Top and Bottom.
Step 5: Artwork Etching Prototype
Here is how i produce my prototype boards
Print the artwork onto some heavy tracing paper. I use 63 GSM from smiths, cheap and cheerful.
The printer is key here. You want as opaque as possible and as clean as possible.My laser printer is getting on now but its good for printing down to 10 thou, less than that you need some specialist gear as the grain of the tracing paper starts getting in the way. You can buy specialist paper but hey that's too good for the likes of me.Any way i'm constantly refining my designs so it would be too expensive for little ole me. If you need a proper board then ask the experts to do it.
I normally prototype my boards in sections and then produce a final with gerbers for an internet company to produce.The prototype sorts out all those errors and omissions before committing real money.
After you have printed the images onto two separate A4 sheets of tracing then cut them so that they both overlay and sellotape them into position. leave enough room for your double sided board to squeeze in between the layers and make sure the printed side is against the pcb.Make a couple of slits to allow you to slide the boards in and sellotape it temporarily. If you are careful you can overlap the sellotape to allow you to unpick it without destroying the tracing image.
Lay the board with one side down in your UV box. NB I made mine using four new 13W UV lamps and some old control gear and made a box with a clear glass panel. Spacing was experimental. The lid clips down and squeezes the foam onto the back of the board forcing the mask against the glass. If you fail to do this the light will undercut the etch resist. Switch on and expose the board for 1 minute 40 secs per side. Use your watch. Actually i must modify this with a timer on it...oh no another project...perhaps i'll buy one...you know you wont!
The boards i purchase from Mega Electronics are the cheap prototype boards with etch resist. The etch resist is sometimes a bit patchy but i have some boards i have had for four years and they still produce good boards for prototyping!!
Careful as you turn the board over, dont let it slip or you will have some junk.
Now prepare your developer. I use 2 capfuls at 18:1 so thats two caps of developer and 36 caps of pure unadulterated tap water. Developer comes in a bottle or powder form and a bottle usually last s me about 6 years!!. Mix it in a ice cream container AND WEAR GLOVES. Do not put your fingers in it or the grease will affect it. Temp can be a problem in winter. Keep the water at 20 degrees or thereabouts, its not critical but if its cold the results can be unpredictable. Ive done this in January in my garage with a kettle so you can compensate.
Gently move the container from side to side once you have put your board in it, If you have done it right you will see a Magenta stream of etch resist being removed from the copper revealing a lovely shiny copper between the tracks. Turn it over using your gloves to check the other side.This process normally takes about a minute tops so when complete take the board out and rinse thoroughly in warm water. If its dull then chances are its failed.
I have had this a couple of times but its usually because i failed to wear gloves or the temp was too low or i messed up the exposure time because of some distraction...why do they do this in the middle of something critical....
Show Me Your Etchings
Ok so now you have a board with beautiful green tracks protected by etch resist and now need to make up the Ferric Chloride. Now I use the same ice cream container to minimise waste and mix up to the notes on the packet. I buy the Ferric Chloride in packets of balls that you weigh out and dissolve into a solution. Make up enough to fill the ice cream tub about 1/3 of the way up.If you make up just enough for the job then you can pour into a plastic container and it will keep for a long while.
YOU MUST WEAR GLOVES...not because of grease etc but because if you dont your hands will be bright orange for a fortnight. I once did this before an important meeting in London and it looked like i had been tangoed.However be aware this stuff is nasty around anything copper...and anything really. Wear old clothes because if it gets on them they are trashed. It attaches itself to stainless steel sinks and generally stains everything. Do this outside or in an outhouse away from everything.
Do not pour the leftovers down the sewers, it is great at killing bacteria which is not what the water authority would like to promote in their septic tanks. For some authorities this is also illegal so dont do it.
When you first dip your board into the solution it will go a lovely red bronze colour as the copper is attacked. If it doesn't then you may still have a layer of etch resist that is preventing the etchant from working or your solution is wrong. If that's the case then back to the drawing board i'm afraid, but its not likely your etchant is wrong if you followed the destructions.
Anyway if all is well you have some perfect tracks protected by etch resist.
Normally at this point the professional would use a set of masks to develop around the holes and pads and produce the via copper through plus use a silk screen for painting component numbers etc.. Unfortunately i do not have the time or inclination and make my vias using some copper wire from layer to layer...obviously multi layer is a no no by this method. If i'm in the mood i will painstakingly add component numbers to the print layer so you etch the component numbers as well. It can save some time in assembly but depends on how busy the board is.
I now expose the tracks for another 2 mins or so per side and dip in the developer to remove all the etch resist.
I now drill the vias and any through holes and tin all the tracks and check continuity with a multimeter.
This part is a ball ache and you would not normally do for a professional produced board with true vias but its worth doing to avoid the via wire being only soldered one side...happened so many times!
Step 6: Assembling the Board
OK so now i have a board ready to go and i have drilled all the vias and through holes.
Thread all the vias with wire and solder both sides. i like to tin all the tracks as a protection dont have to do it but stops the copper oxidising.
I attached the sd card which is surface mount and added two lands to get a tack of solder on it plus it has a couple of pins underside to anchor it down.
Next added the ADXL EEPROM etc.
Power wise the plan is to take the 5V from the USB or external Bat and feed it through the 3.3V reg on the board.
I have a little reg i bought from Ebay which is assembled as a module and rated at 800mA...thats 300 gretaer than USB can provide. I have not done any power measurements yet as may tweak in software.....might use INACTIVITY interrupt from ADXL to deal with.[WIP]
...need to add pictures as build progresses.
Step 7: CODING
OK I wont go through all the arduino set up as someone else has made a much better job of it elsewhere.
I will just pick out the pertinent bits as we go which can be used in a project of your own.
This is still work in progress as far as upload to the website is considered but lets go through some of the code.
The idea is that the normal functionality is init the devices which consist of GPS /display/sd card/real time clock /ext_flashmemory and accelerometer.
After that we enter a loop waiting to see if the interrupt key sw is pressed. If it is then jump to the configurator menu to configure the wifi network to auto log onto a device specified as an input to the web page configurator.
First it scans all devices for an ssid and then allows you to select one and saves it to flash with a flag to indicate that on next boot load the settings from flash. You can always override this by entering the ipaddress and typing /killbill to initate a clean out of the flash and load default.
here is the interrupt function defined in the code for the adxl accelerometer and the jump to configurator on two separate pins. We are using the ADXL interrupt to interrupt on SHOCK event.
For both interrupts we set a flag that is reset in the main "loop". here is what it looks like:
look at the attached code
Step 8: Finding SDA SCL Pins
use this code:
prints the pins currently configured in the ESP for SDL and SCL
There is a file that is used for mapping pins to functions such as serial ports in the ESP32