Arduino Serial Data to Excel - Page 1
Forum section: electronics / beginners

author: metrologist
author_rank: Super Contributor
timestamp: July 12, 2017, 07:45:53 pm
content: I've come to using PuTTy to capture Arduino's serial data to a log file, and then I have a data connection from Excel to that log file for plotting.(I am reading an analog voltage and printing "millis, value" to serial every 200 ms, it's a very simple program that works great for what I want)Two problems:The voltage I want to measure is likely to range between 25V to 45V. I thought of using a 10k potentiometer to create a voltage divider, but then I lose one bit of resolution (that would map to about 2.2V to 5V). I could ignore this, but it would be good to have the resolution, and I also wonder if there is a better range for the adc, such as 1V to 4V rather than say 2V to 5V?Ever so often, there is a data glitch, it looks like the data in a cell gets truncated or shifted one cell to the right. I suspect this might be Excel accessing the log file (via auto refresh every 1 min) the same time as PuTTy and interrupting the communication to the log file. I could manually clean the data later or just access the log file after the test is complete, but it is nice to see the data plot update periodically.I had initially hoped for a more direct path into Excel from the com port serial stream and have a more live update. Here are a couple VBA scripts I tried that would work once out of a thousand times or just capture some garbage or nothing. I gave up on them after days of hacking away at found code, but I post them for amusement.Code:Sub Send_and_Read()'this one seemed to work when sending a "plain string, and sometime when casing a static integer variable cast as a string(intVar), for example...'--------------------------------------------------------cmnd$ = "Hello World" 'A string to sendDim i As IntegerDim timeout As Datei = 1'--------------------------------------------------------timeout = Now + TimeValue("00:00:20")Open "COM5" For Binary Access Read Write As #5cmnd$ = cmnd$ + Chr(13) 'add  to command string'Put #1, , cmnd$ 'write string to interface'--------------------------------------------------------While Now < timeoutanswer = "" 'clear response stringchar = Input(3, #5) 'get first characterWhile (char <> Chr(13)) 'loop until If (char > Chr(47) And char < Chr(58)) Then 'was chr 31answer = answer + char 'add, if printable charElse' Do what ever you likeEnd Ifchar = Input(255, #5) 'get the next characterWend'--------------------------------------------------------'Cells(1, 1) = answer 'put response in cell("A1")Cell = "B" & iRange(Cell).Value = NowCell = "C" & iRange(Cell).Value = answeri = i + 1Application.Wait (Now + TimeValue("0:00:01"))WendClose #5End SubPublic Sub Receive_COM5()'This one seemed more reliable in capturing data, but was often 0 or 224.Dim timeout As DateDim byte1 As Byte, chars As String, lineEnding As StringDim comPORT As IntegerDim i As IntegercomPORT = 5i = 1'Define line ending character(s)'lineEnding = vbCrLflineEnding = vbCr'Monitor COM port for 30 secondstimeout = Now + TimeValue("00:00:10")Close comPORT'Open COM5 port with baud rate 9600, No parity, 8 data bits, and 1 stop bit.'These are the default port settings in HyperTerminal.'In HyperTerminal, use Flow control = None.'Open "COM5:9600,N,8,1" For Random As #5 Len = 3Open "COM5:9600,N,8,1" For Binary Access Read Write As #5chars = ""While Now < timeout'Get 1 byteGet #comPORT, , byte1Debug.Print Now; IIf(Chr(byte1) < " ", "<" & byte1 & ">", Chr(byte1))chars = chars & Chr(byte1)Cell = "C" & iRange(Cell).Value = charsi = i + 1Application.Wait (Now + TimeValue("0:00:01"))If Right(chars, Len(lineEnding)) = lineEnding Then'Debug.Print "Line:" & Left(chars, Len(chars) - Len(lineEnding))chars = ""End IfDoEventsWendClose #comPORTEnd Sub

author: metrologist
author_rank: Super Contributor
timestamp: July 12, 2017, 08:04:07 pm
content: BTW, in case I am not thinking something. I plan to connect the leads of a 36V battery pack while it is being charged via a wall charger. The arduino will be connected to a PC via USB, so there will be a ground connection to the PC and the wall charger. Will that create a magnesium glow and burn a hole to the center of the earth?

author: metrologist
author_rank: Super Contributor
timestamp: July 12, 2017, 08:30:28 pm
content: Here is the circuit I've constructed. It looks like there will be flames...

author: The Soulman
author_rank: Super Contributor
timestamp: July 12, 2017, 08:47:22 pm
content: Quote from: metrologist on July 12, 2017, 08:04:07 pmIt will likely stop at the concrete floor, so your carpet may suffer..Nah, if the output of the charger is floating i.e. not connected to earth, you should be fine.Some galvanic separation between the arduino and the pc would be best.Why not use a (cheap) multimeter with serial output? safety and accuracy would be much improved.You can still capture that (optical isolated) data with arduino and log it to a sd card.If you decide to go that route make sure you get a multimeter with a fs9922 chip so I can use that program to.

author: mikerj
author_rank: Super Contributor
timestamp: July 12, 2017, 10:17:09 pm
content: If you have a 32bit version of Excel it's possible to use the MSCOMM32.OCX control that came with Visual Basic 6. You need to do a spot of hex editing on the control if you want to use ports above COM12 (I think, been a long time since I used it).

author: rstofer
author_rank: Super Contributor
timestamp: July 13, 2017, 01:03:46 pm
content: Losing resolution (bits) because of a wide range of input voltages can be overcome with an op amp and 4 resistors. See Chapter 4 here:https://focus.ti.com/lit/an/slod006b/slod006b.pdfFor example, if you wanted to measure the range of 25-42 volts, you would first subtract (offset by) 25V leaving 0-17V. Then you might scale by 4 to get 0-4.25V.Offset and scale, that's one way to not lose so much resolution.

author: sangseu
author_rank: Contributor
timestamp: July 29, 2017, 05:04:24 am
content: My plan.Please see attacked picture

author: gnif
author_rank: Unknown
timestamp: July 29, 2017, 05:56:40 am
content: Why not log into a database, even SQLite would be better then trying to interface to Excel directly. You could then interface from Excel to the SQLite database to import the data, avoiding any 1/2 reads, etc.