American Chemical Society



Simple Visual-aided Automated Titration Using the Python Programming LanguageSong Wei Benjamin Tan, Pavan Kumar Naraharisetti, Siew Kian Chin, Lai Yeng LeeSupporting InformationMaterialsAqueous potassium hydroxide solution (0.1 M) was prepared by diluting a stock solution of 0.5 M potassium hydroxide (Potassium hydroxide 0.5 mol/L (0.5 N) in aqueous solution, AVS TITRINORM volumetric solution, VWR Chemicals) with deionized water in a volumetric flask. Aqueous hydrochloric acid (0.1 M) was prepared by diluting a stock solution of 0.5 M hydrochloric acid (Hydrochloric acid 0.5 mol/L (0.5 N), AVS TITRINORM Reag. Ph. Eur. volumetric solution, VWR Chemicals) with deionized water in a volumetric flask. Phenolphthalein solution (0.5 wt.% water:ethanol(1:1), Sigma-Aldrich) was diluted two-fold with water:ethanol (1:1) before use. For the purpose of this technology report, the diluted aqueous potassium and hydrochloric acid were not standardized as part of the experiment.Table S1: Equipment and parts requiredEquipment and partsRemarks1/8” outer diameter PTFE tubing-Luer Adapter 1/4-28 Female to Female Luer, PEEKIDEX Health & Science, Part # P-6581/8” Inverted Cone Fitting 1/4-28 BlueDiba Industries Ltd., Part # 008NC32-YC5UFlangeless Ferrule TefzelTM (ETFE),1/4-28 Flat-Bottom, 1/8” OD NaturalIDEX Health & Science, Part # P-34010 mL disposable plastic syringe-Glass test tube-10 mm PTFE stir bar-Syringe pumpHarvard Apparatus Pump 11 EliteUSB Web CameraLogitech C310 HD WebcamMagnetic StirrerHeidolph 3001 series, MR3001KLaptopCompatible for installing Anaconda DistributionDetailed information on assembly of the ferrule onto PTFE tubing has been described by Britton and Jamison. (Britton, J.; Jamison, T. F. The assembly and use of continuous flow systems for chemical synthesis. Nat. Protoc. 2017, 12, 2423-2446.)A syringe pump that is controllable via serial command is essential for this setup. An alternative syringe pump is the NE-1000 from New Era Syringe Pump Inc. ( ; accessed Oct 2019).Automated Titrator Setup1. Checking for Screen ResolutionFor Windows 7 (2009):Go to Control Panel > Appearance and Personalization > Display > Adjust ResolutionFor Windows 10:Go to Start > Settings icon > DisplayA typical screen resolution for 15” laptop is 1366 x 768 pixels.2. Installing AnacondaRefer to (last accessed January 2019) for instructions to install the Anaconda Distribution software onto laptop/computer.3. Setting Up of Additional Python PackagesTwo additional Python packages need to be installed. “Pyserial” allows for communication between the computer and the syringe pump. “Pyautogui” allows for the determination of the RGB color of a given pixel on the computer screen.To install these packages, access “Anaconda Prompt” from the Start bar menu folder and type in the respective commands:PySerialconda install –c anaconda pyserialPyautoguiconda install -c conda-forge pyautogui4. Setting Up of ApparatusThe setup for the automated titrator is shown in Figure S1 below.Figure S1. Setup of the automated titrator. A piece of white paper is used to provide the white background for the color detection using the web camera.5. Installing Device DriversInstall the drivers for web camera and syringe pump onto the computer, referring to the respective instruction manuals for more details. After installation of drivers are completed, the syringe pump is connected to the computer via a USB cable. Go to Device Manager and expand the Port (COM &LPT) to determine the COM Port assigned to the syringe pump by the computer.The Port settings could be checked by going to the Properties sub-section of the device. The baudrate (bits per second), data bits, parity, stop bits and flow control are to be noted.6. Determining Pixel Coordinates on the Screen and RGB Color ValuesThe Pyautogui package allows the pixel coordinates and the RGB color data to be obtained from any point on the computer screen.The following Python code could be used to obtain the pixel coordinates at any particular location the mouse cursor is pointing, along with its corresponding RGB color values. The Spyder Integrated Development Environment (IDE), a software in the Anaconda Distribution Package, is used to run the Python code. Copy the following code and save as “get_pixel_coordinate_RGB.py’. get_pixel_coordinate_RGB.py1import pyautogui2import time34#mapping screen resolution to pyautogui5pyautogui.size()6(1366, 768) #change this according to the screen resolution obtained from Section 1 above7width, height = pyautogui.size()89im = pyautogui.screenshot()1011#the pixel coordinate at which the mouse cursor is pointing would be printed once every 2.5 second, along with its RGB values12while True:13 try:14 pix_coord = pyautogui.position()15 print (pix_coord)16 RGB = im.getpixel(pix_coord)17 print ("pixel RGB " + str(RGB))18 time.sleep(2.5)19 except KeyboardInterrupt:20 breakA manual acid-base titration is to be done, and 8 mL of the pink end point solution is transferred to a test tube used for the calibration as follows.Place the test tube with the pink end point solution on the setup. Open the web camera window on the desktop. The window from the web camera displayed on the computer screen is used to provide the feedback to the Python code. A position on the desktop screen for the web camera display window is chosen. The web camera display window should also not be moved for the remainder of the experiment.Adjust the position of the web camera such that the view of the test tube is centered. The relative position between the web camera and the setup should not be adjusted for the remainder of the experiment. Switch off the “auto white balance” function (or equivalent) of the web camera if the option is available, to minimize errors associated with detecting the RGB values.Run the code “get_pixel_coordinate_RGB.py” with Spyder IDE. Choose two points over the pink coloration by positioning the mouse cursor over it to obtain the pixel coordinates, along with its corresponding RGB values. Note the two chosen pixel coordinates together with their RGB values. In principle, any two pixels located over the region with the pink solution could be chosen. Due to reflection from the curved test tube surface, some parts of the image may appear as a lighter shade in color. It is advised to choose two pixel coordinates that are reasonably apart, yet have RGB values within 15 units for each of the color channels. In such a case, a tolerance value of 30 would be deemed reasonable. (See Section 7 below)Figure S2. An example of the web camera showing a centered view of the test tube with the pink solution. The web camera display window is fixed on the right side of the desktop screen. Two pixel coordinates corresponding to the position occupied by the solution is chosen (shown by the positions of red diamonds approximately) by positioning the mouse cursor over it (mouse cursor not shown). The pixel coordinates and RGB values will be output in the IPython console (marked by red box). Here, the pixel coordinate at (986, 292) has a RGB value of (176, 114, 160); the pixel coordinate at (958, 326) has a RGB value of (175, 122, 163).Once the pixel coordinates and RGB values have been obtained, click on the IPython console sub-window of the Spyder main program window, followed by pressing “Crtl + c” on the keyboard to stop the code. Alternatively, go to Console > Restart kernel within the Spyder main program window to exit the code.7. Automated Titration using Python CodeThe Python code is provided as a .txt file (auto_titrator.txt). Open and save the file as a .py file (auto_titrator.py) prior running the Python code (auto_titrator.py) in Spyder. Update the following lines in the code based on the configuration used, if required.Lines 7-8: Depending on where exactly the web camera window is placed on the screen and the position of the test tube relative to the window, the pixel coordinates of the two chosen points should be updated. See Section 6 on obtaining the pixel coordinates.Line 11: The RGB values of either of the chosen pixel coordinates. See Section 6 on obtaining the RGB values. Line 14: A tolerance of 30 is set. (Tolerance could be considered as the ‘deviation’ from the desired value that would still be taken as an accepted value. The value for tolerance to be used depends on the actual lighting conditions in the fumehood, which determines how similar the RGB values of the two pixel coordinates chosen earlier. A value of 30 is a recommended value).Line 18: update COM port number that the syringe pump is detected (see Section 5).Line 19-26: port settings of COM port (see Section 5).Line 37: update screen resolution if dimensions are not 1366 x 768 pixels (see Section 1).For the Harvard Apparatus Pump 11 Elite syringe pump, the following are the serial commands used to control the pump; more commands could be found in the equipment user manual.Line 68 - 'irate 2 ml/min\r': to set the flow rate on the syringe pump at 2 mL/min.Line 69 - 'irun\r': to start the infusion of the syringe pump.Line 74 - 'stp\r': to stop the syringe pump. Set up the apparatus by placing 4.0 mL of aqueous hydrochloric acid analyte and phenolphthalein indicator in the boiling tube with a PTFE stir bar. Fill the 10 mL disposable syringe with aqueous potassium hydroxide titrant, and prime the PTFE tubing. Ensure there are no bubbles in the PTFE tubing or syringe.Run the Python code when ready. The addition of the titrant by the syringe pump would stop automatically once the desired pink coloration is detected through the projected image by the web camera onto the computer screen.Application of Setup in an Instructional LaboratorySuggestion for Segmenting the Python Code (auto_titrator.py)The manual titration process involves the addition of titrant, mixing the solution, and checking for a change in the indicator color. This process is repeated until the end point color change is obtained. The concept of automation is introduced as a means to perform this iterative process, through the use of common principles in computer science such as declaration and assignment of a variable, looping structure, control structure, printing output and threading.To facilitate application of the knowledge in coding, the code used in this setup could be segmented to suit the students’ proficiency level with the Python programming language. Students would then be given these blocks of code in a randomized order, which is to be assembled in the correct sequence for the program to function as intended. An example of the segmented truncated Python code is as shown in the next page:-65314107859import … …-5937715776900{inputs for pixel coordinates}{inputs for RGB color value of the desired end point}{inputs for tolerance}-647701582060{setup for pump serial communications}-6369014859000{mapping of screen resolution}-319121219200def detect_RGB():-325471498600while True:{set screen pixel coordinate to get RGB values}{set end point RGB values} -322231238250if {RGB values} == {endpoint RGB}:…print (“End point reached!”)break-323851465940def pump_crtl():-321951416050{set pump rate, and start pump}-321951606550while True:if {RGB values} == {endpoint RGB}:{stop pump}break-326571096740if __name__ == “__main__”:{start two threads, one for each function as defined above}{close thread when done}Appendix 1: Pump calibration dataThe syringe is filled with deionized water (Density = 1.0 g/mL), and the PTFE line primed. Water samples were collected for 120 s each with a flow rate at 0.8 mL/min, while samples were collected for 60 s each with flow rates of 2.0 mL/min and 3.5 mL/min.Table A1. Pump Calibration DataFlow rate = 0.8 mL/minReplicateAverage volume, mL (N=3)Standard Deviation123Displayed volume (mL)1.5971.6041.595——Measured weight (g)1.58581.59081.5831——% error (w.r.t displayed)-0.70131-0.82294-0.74608-0.7570.062Flow rate = 2.0 mL/minReplicateAverage volume, mL (N=3)Standard Deviation123Displayed volume (mL)1.9812.011.979——Measured weight (g)1.96841.99671.9689——% error (w.r.t displayed)-0.63604-0.66169-0.51036-0.6030.081Flow rate = 3.5 mL/minReplicateAverage volume, mL (N=3)Standard Deviation123Displayed volume (mL)3.4743.4973.46——Measured weight (g)3.45563.47513.4282——% error (w.r.t displayed)-0.52965-0.62625-0.91908-0.6920.203Appendix 2: Automated titration results with different flow ratesThe average titrant volume dispensed, and standard deviation, with the automated titrator setup at three different flow rates were obtained. The corrected volume of KOH was calculated taking into account the average % error as shown in Appendix 1 for the corresponding flow rates. The stirring rate was kept constant at 750 rpm.Table A2. KOH Titrant Average Volume for Automated TitrationFlow rate = 0.8 mL/minReplicateAverage volume, mL (N=5)Standard Deviation12345Vol. of 0.1 M KOH added (mL)4.154.2154.1854.2254.21——Corrected vol. of KOH (mL)4.1194.1834.1534.1934.1784.1650.030Flow rate = 2.0 mL/minReplicateAverage volume, mL (N=5)Standard Deviation12345Vol. of 0.1 M KOH added (mL)4.1344.1724.1714.2084.143——Corrected vol. of KOH (mL)4.1094.1474.1464.1834.1184.1400.029Flow rate = 3.5 mL/minReplicateAverage volume, mL (N=5)Standard Deviation12345Vol. of 0.1 M KOH added (mL)4.2844.2714.2964.3244.269——Corrected vol. of KOH (mL)4.2544.2414.2664.2944.2394.2590.022Appendix 3: Manual titration results Manual titration was conducted by dispensing 0.1 M KOH from a graduated 50 mL burette to a 15.0 mL solution of 0.1 M HCl in a conical flask. The average titrant volume dispensed and standard deviation using the manual titration setup were obtained. Table A3. KOH Titrant Average Volume for Manual TitrationReplicateAverage volume, mL (N=5)Standard Deviation12345Starting volume of 0.1 M KOH (mL)2.5017.707.2022.4029.20——Final volume of 0.1 M KOH (mL)17.7032.8522.4037.6044.40——Vol. of 0.1 M KOH added (mL)15.2015.1515.2015.2015.2015.190.02Figure A3-1. Photo of the end point solution obtained using a manual titration setup. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download