반응형

【 Leonardo #3 Visualize Temperature& Brightness Data on your PC with Leonardo

 Using the Arduino Leonardo board, you can store data from a temperature sensor and a light sensor (CDS) in real time on a PC. This data can be visualized through the Processing IDE.
Let's have fun!   

 

▶ Pre-learning :
      1. 【 Leonardo #1】 Let's collect Temperature & Brightness data to PC! ☜ (Click)

 

▶ The Goal  

1. Screen size (width x height pixels): 500 x 500)You can connect and operate the temperature sensor (Tmp36) and the light sensor (cds) circuit on the Leonardo board.

2. Background color and lines: background (180), stroke (0), strokeWeight (5), fill (255, 20)You can send data from the temperature sensor and the light sensor to the notepad.

3. Circle drawing uses ellipse (x, y, diam, diam) functionTransmitted data can be visualized with processing IDE coding.

 

▶ 실습 목표 :  
 1. [ 레오나르도 보드에 온도센서(Tmp36)와 광센서(cds) 회로를 연결하고 동작시킬 수 있다.
 2. [ 온도센서와 광센서로 부터의 데이터 값을 PC메모장을 통해 전송 시킬 수 있다.]
 3. [ 전송된 데이터를 엑셀 등을 이용해서 활용해 볼 수 있다 ]
 

 4. [ 전송된 데이터를 프로세싱 IDE 코딩을 통해 보기 좋게 비주얼화 시킬 수 있다. ] 

 

▶ Connection diagram :
  ( Click image to enlarge )

▶ 실행 결과 이미지 :

온도 밝기 센싱값 출력화면

 【Arduino Coding】  : 
/* Leonardo board Transfer temperature & brightness data to PC with Leonardo board */

#include <Keyboard.h>   
const int TEMP = 0;    // A0 port
const int CDS = 1;    // A1 port  
const int LED = 12; 
const int BUTTON = 11; 
boolean lastBtn = LOW; 
boolean currentBtn = LOW; 
boolean running = false; 
int counter = 1;

void setup ( ) {  
  pinMode(LED, OUTPUT);
  Keyboard.begin();
  Serial.begin();
}

void loop ( )  {
  currentBtn = debounce (lastBtn); 
  if (lastBtn == LOW && currentBtn == HIGH) //Click the button
  { 
     running = !running;   // Change state values backwards 
   } 
  lastBtn = currentBtn; // Update button state values
  if (running) // Writing data 
  { 
     digitalWrite(LED, HIGH); 
     // The millis () function returns the data in ms.
     // So this function is used when the delay () function is not available
   if (millis()%1000 == 0) // Execute if statement every second
   { 
      int temperature = analogRead(TEMP); 
      int  brightness = analogRead(CDS);
      float mVoltage = temperature*5000.0/1024.0; //Converted to Celsius temperature 
      float TempDotC = (mVoltage - 500) / 10.0;   //Converted to Celsius temperature
      Keyboard.print(counter);  // Data numbering before each line 
      Keyboard.print(",");
      Keyboard.print(TempDotC);  // Temperature data
      Keyboard.print(",");
      Keyboard.print(brightness);   // Brightness data
      Keyboard.print("\n");
      counter++;
    }     
  } 
  else 
  { 
    digitalWrite(LED, LOW); 
  } 
}

// Generate subfunctions to prevent button bouncing
boolean debounce (boolean last) 
{ 
  boolean current = digitalRead (BUTTON); 
  if(last != current) 
  { 
    delay(5); 
    current = digitalRead(BUTTON); 
  } 
  return current;     
}

▶ Arduino Code download : 
( The code is zip compressed.)

Eng_TMP36CDStoPC_Arduino-code.zip
0.00MB

---------------------------------------------------------------------------------

 

 【Processing Coding】  : 
/* Visualizing Temperature & Brightness Data on a PC */

PImage imgCDS;
PImage imgTemp;
PFont font;
import processing.serial.*;
Serial port;
String temp ="";
String br ="";
String data="";
int index=0;

void setup () {
  size(800, 600);
  background(255);
  port =new Serial(this, "COM21", 9600);
  port.bufferUntil('.');
  font=loadFont("Arial-Black-50.vlw"); 
  textFont(font, 200);  
}
void draw() {
  background(255,255,255);
  imgTemp = loadImage("Temp1.png");
  imgCDS = loadImage("cds1.png");
  image(imgTemp, 0, 0);
  image(imgCDS, 450, 10, 250,250);    

  fill(150, 150, 10);
//  textSize(80);
  text(temp, 70, 500);
  fill(50, 0, 200);
  text(br, 450, 500);
}
void serialEvent(Serial port) {
  data=port.readStringUntil('.');
  data=data.substring(0, data.length()-1);
  index=data.indexOf(",");
  temp=data.substring(0, index);
  br=data.substring(index+1,data.length());
}

 

▶ Download Processing code :
( The code is zip compressed.)

imageTempCDS (Ras).zip
4.87MB

 

▶ Making Processing Font
(In order to use the window font on the processing output screen, it is necessary to convert the bitmap font into a usable bitmap font in the following process)

[Tools menu  》 "Create font"  》 Select font (size)  》 "Click OK"  》 Copy file name  》 "Paste into processing code]

 

▶ Video lecture :  
(Look in full screen view)

(youTube)

https://youtu.be/1E1DgOqXY2o

 

 

Troubleshooting :  
 If you get the following error when running a sketch in the Arduino IDE: 

 

If the above error appears, you need to confirm that you have selected the board as a Leonardo board from the board menu under Tools menu in the Arduino sketch. If the board selection is not selected by Leonardo, it will not be able to run even if it is compiled, and this will cause a 'Keyboard' error.

 

 

반응형
반응형

【 Leonardo #1 Let's collect Temperature & Brightness value to PC!

Welcome to RasINO~!   
In this chapter, we will learn how to use Arduino Leonardo board.    Let's go~!

▶ The Goal : 
 1. You can see how to use the Leonardo board.
 2. You can transfer the data from the temperature sensor & optical sensor (CDS) to the PC.
 3. You can make your data into Excel charts or use them in a variety of ways.

▶ Connection diagram :
  ( Click image to enlarge )

 

▶ Coding and annotation : 
/* Leonardo board Transfer temperature & brightness data to PC with Leonardo board */

#include <Keyboard.h>   
const int TEMP = 0;    // A0 port 
const int CDS = 1;    // A1 port  
const int LED = 12; 
const int BUTTON = 11; 
boolean lastBtn = LOW; 
boolean currentBtn = LOW; 
boolean running = false; 
int counter = 1;

void setup ( ) {  
  pinMode(LED, OUTPUT);
  Keyboard.begin();
}

void loop ( )  {
  currentBtn = debounce (lastBtn); 
  if (lastBtn == LOW && currentBtn == HIGH) // Click the button
  { 
     running = !running;   // Change state values backwards
   } 
  lastBtn = currentBtn; // Update button state values 
  if (running) // Writing data
  { 
     digitalWrite(LED, HIGH); 
     // The 'millis()' function returns the data in ms. 
     // So this function is used when the 'delay()' function is not available 
   if (millis()%1000 == 0) // Execute if statement every second
   { 
      int temperature = analogRead(TEMP); 
      int  brightness = analogRead(CDS);
      float mVoltage = temperature*5000.0/1024.0; // Converted to Celsius temperature 
      float TempDotC = (mVoltage - 500) / 10.0;   // Converted to Celsius temperature 
      Keyboard.print(counter);  // Data numbering before each line 
      Keyboard.print(",");
      Keyboard.print(TempDotC);  // Temperature data
      Keyboard.print(",");
      Keyboard.print(brightness);   // Brightness data
      Keyboard.print("\n");
      counter++;
    }     
  } 
  else 
  { 
    digitalWrite(LED, LOW); 
  } 
}

// Generate subfunctions to prevent button bouncing 
boolean debounce (boolean last) 
{ 
  boolean current = digitalRead (BUTTON); 
  if(last != current) 
  { 
    delay(5); 
    current = digitalRead(BUTTON); 
  } 
  return current;     
}

▶ Code download : 
( The code is zip compressed.)

Eng_TMP36_CDS_to_PC.zip
0.00MB

 

▶ Video lecture :  
(Look in full screen view)

 

(You can watch it on YouTube.)
https://youtu.be/K-Dc5MqBYaI

 

반응형
반응형

【 레오나르도활용#1】 아두이노 레오나르도 보드 사용법 #1(Leonardo

▶ 아두이노 Leonardo보드는 아두이노 우노(Uno) 계열의 보드로서, 특징은 PC와 연동하여 활용하기 좋은 보드이다. 기본적인 기능이나 스펙은 우노 보드와 유사하다.  하지만, 우노 보드와 달리 PC와 연결하면 마우스, 키보드와 같은 가상 직렬 포트로 인식되어  PC 제어가 쉬운 특징이 있다.  우노에서의 USB 시리얼 통신(FTDI) 담당 칩인 Mega16u2를 제거하고 그 대신, USB 처리 기능이 있는 프로세서 칩인 ATmega32U4를 사용하였다. 

※ Leonardo는 PC 연결 시, 가상 직렬포트(CDC)로 인식되어, 키보드나 마우스와 같은 HID(Human Interface Device) 장치로 작동 가능한 특징이 있어 키보드 또는 마우스 클래스를 이용하여, PC를 제어하는 용도의 프로젝트에 강점이 있다.

우선, 먼저 레오나르도(Leonardo)에 대해 자세히 알아보고 연결해보도록(드라이버 설치) 하겠다 

아두이노 레오나르도

▶ 레오나르도 사양 :     

Leonardo 상세 사양

▶ 레오나르도 연결(드라이버 설치) :    

 레오나르도를 처음 연결하게 되면 장치가 인식되지 않는데, 
윈도 시작메뉴시작 메뉴로 가서,  제어판 》 장치 관리자 》 기타 장치  쪽을 살펴봐야 한다.

 아래처럼 주의 표시나, 알 수 없는 장치 표시가 뜨게 된다.

그럼 이제 마우스 우클릭하여 드라이버 설치를 진행해보자.

 그럼 아래처럼 드라이브 업데이트 방법에 대한 선택 창이 뜨는데, 
검색으로 찾지 말고, 컴퓨터에서 드라이버 소프트웨어 검색을 눌러준다.

그다음, '찾아보기(R)' 버튼을 눌러,  드라이버의 위치를 아두이노가 설치되어 있는 폴더 아래에 보면 drivers라는 폴더가 있는데, 그 폴더로 선택해준다. (아두이노가 설치된 위치는 다를 수 있음)
 그런데, 이렇게 지정하여도 드라이버 설치가 제대로 안 되는 경우가 있는데, 아두이노 IDE를 너무 오래전 버전을 사용하게 되면 그럴 수 있다.  따라서, 아두이노 사이트에서 아두이노IDE를 최신 버전으로 업데이트해보면 잘 될 것이다. 

 

그러면 드디어 아래처럼, 'Arduino Leonardo(COMxx)' 라며 선명하게 잡히는 것을 볼 수 있을 것이다.


▶ 
레오나르도 특징 :

 아두이노 우노 보드에서는,  보드와 PC 사이 통신(USB to Serial)을 담당해주는 보조 칩인 "mega 16u2"가 따로 존재한다.
하지만, 아두이노 레오나르도 보드에서는 메인칩을 'ATmega32 U4'로 바꾸면서 그 기능까지 담당하게 하였다. 
덕분에 레오나르도 보드의 가격이 우노보다 저렴해진 장점이 있다.  하지만, PC와 연결된 상태에서 리셋 버튼을 누르게 되면 PC와 연결이 끊겼다가 다시 연결되는데, 이 과정에서 장치 목록이 리프레쉬되고 시리얼 통신 중인 연결(데이터)이 끊기게 된다.  별도의 칩이 있어 리셋 버튼에 영향을 받지 않는 우노와 다른 점이다.   
 그래서 
스케치(IDE) 코드를 업로드하거나, 통신을 할 때 영향을 줄 수 있으니 레오나르도 보드를 사용할 때는 약간의 주의가 필요하다.  예를 들면,  레오나르도 보드에서는 PC에서 직렬(Serial) 포트를 열어도 스케치를 다시 시작하지 않는다. 즉, 보드에 의해 PC로 이미 전송된 시리얼 데이터는 (예, Setup { } 문의 내용들) 처음 순간 금방 전송되어 버리고 나면, 다시 확인할 수 없다.  이후 루프(loop { }  ) 안에 있는 내용만 시리얼 창을 통해 확인 가능하게 된다.

 하지만, 큰 장점도 있다. 하나의 메인 CPU로 스케치를 실행하고 USB 연결도 담당하니, 컴퓨터와의 유연성이 증가하게 되고, 펌웨어를 통해서 USB의 다양한 기능을 사용할 수 있어서 강력한 제어가 가능해진다. 

 그리고, 레오나르도 보드의 ATmega32U4 칩에는 D0(RX)핀, D1(TX)핀에서 사용할 수 있는 USB를 통한 UART TTL(5V) 시리얼 통신 기능을 지원한다.  이 것은 PC로 하여금 레오나르도 보드를 USB(2.0) (가상) 장치로 인식할 수 있게 해 준다. 
이는 아두이노(레오나르도) 보드를 이용해서 마우스나 키보드와 같은 기능을 구현할 수 있음을 뜻한다. 

 또한, 물리적인 시리얼 포트와 가상 COM 포트가 분리되어 있기 때문에, RX, TX단자에 블루투스와 같은 통신 모듈을 연결하여 사용하더라도, 스케치에 데이터를 업로드할 때 장치를 분리하지 않고도 업로드가 가능하다. (우노에서는, 블루투스가 연결되어 동작(통신)되고 있는 상태에서는 스케치를 통해서 업로드가 되지 않기 때문에 PC↔우노↔블루투스 형태의 통신을 위해 우노와 블루투스는 일반 포트를 이용한 소프트웨어 시리얼 통신을 이용하게 된다)

또 하나의 장점으로 위 테이블 표에서도 알 수 있듯이, 레오나르도 보드는 소프트웨어적으로 아두이노 보다 더 많은 총, 20 개의 Digital 입출력 핀과,  12개의 Analog 입력 핀을 설정하여 사용할 수 있다.  (물리적인, 핀 수는 우노와 동일하다)

레오나르도 보드의 뒷면을 살펴보라

위 이미지들은 사용할 수 있는 포트를 명기해 놓은 이미지이며, 보기에 편한 이미지를 사용하면 된다. 

많이 복잡해 보여도 실제 코딩에서는 관련 헤더 파일의 함수와 클래스를 이용하면 쉽게 다룰 수 있다.

그럼, 다음 회차부터는 본격적으로 레오나르도 보드를 사용해 재미나는 회로를 만들어 보려고 한다. 

 

반응형