티스토리 뷰
아두이노 생체신호 센서 Data 파이썬으로 받아서 .csv 저장하기
0.센서
gsr, pulse sensor
https://ing-min.tistory.com/204
아두이노 SEN-11574 심박 센서
https://surtrtech.com/2018/12/28/interfacing-1-heart-pulse-beat-sensor-amped-sen-11574-with-arduino/ Interfacing 1$ Heart Pulse / Beat sensor AMPED SEN 11574 with Arduino Hello, and welcome to this tutorial where we try to use a 1$ Heart pulse sensor, whic
ing-min.tistory.com
1. 아두이노 코드
const int GSR = A2; // GSR 센서 연결 핀
const int PulseSensorPin = A0; // 펄스 센서 연결 핀
int gsr_average = 0; // GSR 신호 평균값
int pulse_value = 0; // 펄스 센서 값
void setup() {
Serial.begin(9600); // 시리얼 통신 시작
}
void loop() {
long sum_gsr = 0; // GSR 신호의 합계를 저장할 변수
long sum_pulse = 0; // 펄스 신호의 합계를 저장할 변수
// GSR 신호를 1초에 24번 받아옴
for(int i = 0; i < 24; i++) {
int sensorValue = analogRead(GSR); // GSR 센서 값 읽기
sum_gsr += sensorValue; // 합계에 추가
delay(42); // 약 1/24초 (1000ms / 24 = 41.67ms)
}
gsr_average = sum_gsr / 24; // GSR 신호의 평균값 계산
// 펄스 신호 읽기
for(int i = 0; i < 24; i++) {
int pulseValue = analogRead(PulseSensorPin); // 펄스 센서 값 읽기
sum_pulse += pulseValue; // 합계에 추가
delay(42); // 약 1/24초 (1000ms / 24 = 41.67ms)
}
pulse_value = sum_pulse / 24; // 펄스 신호의 평균값 계산
// 결과 출력
Serial.print("GSR: ");
Serial.print(gsr_average);
Serial.print(", Pulse: ");
Serial.println(pulse_value);
}
2.py 코드 (visual code 환경에서 작업)
import serial
import csv
import time
import msvcrt
import os # 파일 및 디렉토리 관리를 위한 os 모듈
# 데이터 폴더 생성
data_folder = 'data'
if not os.path.exists(data_folder):
os.makedirs(data_folder)
# 아두이노와의 시리얼 통신 설정
arduino_port = 'COM3'
baud_rate = 9600
ser = serial.Serial(arduino_port, baud_rate)
time.sleep(2)
# 실험자 번호 입력 받기
participant_number = input("Enter participant number: ").zfill(2) # 실험자 번호를 두 자리로 지정하고 앞에 0을 채움
# 데이터 기록을 위한 변수
data = []
record = False
print("Press spacebar to start recording. Press enter to stop.")
while True:
# 사용자 입력 처리
if msvcrt.kbhit():
key = msvcrt.getch()
if key == b' ':
record = True
print("Recording started.")
elif key == b'\r':
record = False
print("Recording stopped.")
break
if record:
# 아두이노로부터 데이터 읽기
arduino_data = ser.readline().decode('ascii').strip()
gsr, pulse = arduino_data.split(",")
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
# 데이터를 리스트에 추가
data.append([timestamp, gsr, pulse])
# CSV 파일로 데이터 저장
filename = f"{data_folder}/recorded_data_{participant_number}.csv" # 파일 이름 생성
with open(filename, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Timestamp", "GSR", "Pulse"])
writer.writerows(data)
print(f"Data saved to {filename}")
4.
1. cmd 실행
2. activate ---가상환경
3. cd C:\Users\------\Desktop\폴더이름
4. python ---파일이름.py
5_0. 실험자 번호 입력 (두자리수)
5. 스페이스바 누르면 레코딩 실행됨
6.엔터누르면 리코딩 종료
7. data 폴더에 csv 파일 생성됨.
5.오류
serial.serialutil.SerialException: could not open port 'COM3': PermissionError(13, '액세스가 거부되었습니다.', None, 5)
>>>>아두이노 시리얼 모니터 끄면됨
'Iot > Arduino' 카테고리의 다른 글
아두이노 조도센서 모듈 [SEN030101] (0) | 2024.02.20 |
---|---|
arduino_스테핑 모터step type 103H5210-0457산요덴키 (0) | 2024.02.20 |
VL53L1X ToF 레이저 거리 측정 센서 모듈_아두이노 (0) | 2024.01.09 |
RedBoard Sparkfun _아두이노 오류 _ 보드 설치 (0) | 2024.01.09 |
아두이노 SEN-11574 맥박 PLUSE센서 (0) | 2024.01.08 |
- Total
- Today
- Yesterday
- Python
- unity 360
- Unity
- oculuspro
- motor controll
- sequelize
- Express
- Midjourney
- VR
- imgtoimg
- houdini
- 후디니
- Arduino
- AI
- TouchDesigner
- Java
- docker
- node.js
- 라즈베리파이
- CNC
- emotive eeg
- ardity
- DeepLeaning
- 유니티플러그인
- three.js
- MQTT
- colab
- RNN
- 유니티
- opencv
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |