Last active
July 2, 2025 03:57
-
-
Save samarlyka/a7a66cbd0007f1565ec85c0301a9697b to your computer and use it in GitHub Desktop.
Instrumentasi (FQ018) TA 2024/2025-3 UKSW - Membaca Serial Output Arduino Menggunakan Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * By Samarthya Lykamanuella on 2023-10-15 | |
| * SOURCE: https://www.circuitbasics.com/how-to-use-photoresistors-to-detect-light-on-an-arduino/ | |
| */ | |
| int PHOTO_PIN = A0; | |
| void setup() { | |
| // put your setup code here, to run once: | |
| Serial.begin(9600); | |
| } | |
| void loop() { | |
| // put your main code here, to run repeatedly: | |
| int light = analogRead(PHOTO_PIN); | |
| Serial.println(light); | |
| delay(100); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import serial | |
| # Hanya bisa di Linux. | |
| s = serial.Serial('/dev/ttyACM0', 9600) | |
| # Kalau Windows, ubah '/dev/ttyACM0' ke 'COM0'. | |
| # Jangan lupa sesuaikan BAUD rate (9600, 115200, dsb.). | |
| s = serial.Serial('COM0', 9600) | |
| while True: | |
| print(s.read_until().decode().strip()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from datetime import datetime as dt | |
| import pandas as pd | |
| import serial | |
| import time | |
| # Hanya bisa di Linux. | |
| # Gunakan "COM0" untuk Windows. | |
| s = serial.Serial('/dev/ttyACM0', 9600) | |
| df = pd.DataFrame({ | |
| 'date': [], | |
| 'time': [], | |
| 'serial_in': [], | |
| }) | |
| while True: | |
| try: | |
| d = str(dt.now()) | |
| date = d.split(' ')[0] | |
| time = d.split(' ')[1] | |
| serial_in = s.read_until().decode().strip() | |
| print(date, time, serial_in) | |
| # Writing the data to external file. | |
| df = df._append( | |
| { | |
| 'date': date, | |
| 'time': time, | |
| 'serial_in': serial_in, | |
| }, | |
| ignore_index=True | |
| ) | |
| except UnicodeDecodeError: | |
| pass | |
| # time.sleep(0.1) | |
| # Write the data. | |
| df.to_csv('sample_001.csv', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment