Arduino中国 | Flamingo EDA

Mar/10

11

FlamingoEDA 开通新浪微博

嫌博客更新太慢的观众不妨去跟踪一下我们在新浪上的微博。

下面两个地址都可以访问:
http://t.sina.com.cn/1704547397
http://t.flamingoeda.com

No tags

在对环境温度与湿度进行测量及控制的时候,瑞士Sensirion公司推出的SHT系列数字温湿度集成传感器无疑是一个非常不错的选择。虽然价格较普通的模拟温度或者湿传感器来讲稍微有点偏高,但在需要读出准确的温度和湿度值的场景下却非常合适,而且具有极高的可靠性和出色的长期稳定性。

我们设计的这款数字温湿度传感器就是基于SHT系列中使用最为广泛的SHT10,有关该传感器的具体参数可以参考官方网站上的介绍

SHT10采用的是一种类似于I2C的两线串行接口(bidirectional 2-wire),因此在物理连接上我们需要使用两根连接线与该电子积木模块连接。

要在Arduino上使用该电子积木,可以从Practical Arduino这本书的官方网站上下载相应的库代码, 或者直接从这里下载我们在在测试时所使用版本的压缩文件。测试时我们使用的是Arduino 0018,只需要将相应的文件解压缩到Arduino安装目录下的libraries目录中就可以了:

测试时将电子积木上的DATA和SCK引脚分别与Arduino的数字I/O的10号引脚和11号引脚连接起来,相应的测试代码如下所示:

#include <SHT1x.h>

#define dataPin  10   // DATA
#define clockPin 11   // SCK
SHT1x sht1x(dataPin, clockPin);

void setup()
{
   Serial.begin(9600);
   Serial.println("Starting up");
}

void loop()
{
  float temp_c;
  float temp_f;
  float humidity;

  // Read values from the sensor
  temp_c = sht1x.readTemperatureC();
  temp_f = sht1x.readTemperatureF();
  humidity = sht1x.readHumidity();

  // Print the values to the serial port
  Serial.print("Temperature: ");
  Serial.print(temp_c, DEC);
  Serial.print("C / ");
  Serial.print(temp_f, DEC);
  Serial.print("F. Humidity: ");
  Serial.print(humidity);
  Serial.println("%");

  delay(2000);
}

下图是相应输出结果:

No tags

我们曾经做过一个八路的数字输出扩展模块,那个时候数字传感器的引脚顺序同现在的通用传感器引脚顺序是不一致的,而且连接的时候要用到三根传感器连接线。近期我们在电子积木的开发过程中引入了IDC接口,这样当电路模块有多个引脚时可以更加方便,不过同时灵活性也就降低了。新一版本的八路数字输出模块就是根据新的传感器引脚顺序,以及多个引脚相连接的问题做了修改:

按照新的传感器引脚的顺序定义,相应的为GND(-),VCC(+)和信号:

连接的时候使用的是IDC扩展板上提供的IDC-6引脚,并通过6芯IDC连接线相连接到八路数字输出模块的595IN插座上:

扩展出来的数字I/O引脚,可以用来连接数字输出模块,比如常用的LED模块:

如果需要多块级连的话,则需要将前一个模块的595OUT座与下一个模块的595IN座相连接:

该模块的核心是一个74HC595芯片,有关Arduino编程的问题可以借用Arduino官网上ShiftOut的说明和例子,但需要按照IDC扩展板上的硬件连线,修改相应的引脚,如下面的代码段所示:

int dataPin = 9;   // SER
int latchPin = 7;  //RCK
int clockPin = 6; //SCK

·

MOSFET是一种具有很好开关特性的电子器件,被广泛应用在需要电子开关的电路中,如开关电源和马达驱动,以及照明调光等。继电器是大家非常熟悉的另外一种具有开关特性的模块,但由于继电器的工作原理一般是靠机械触点来达到开与关的目的,这就必然导致在开关时间非常短的情况之下,继电器无法工作的情况,另外触点开关时发出的叭叭声在某些场合下也是比较让人烦恼的一件事情。

我们设计的这一4路MOSFET开关最多可以提供4组电子开关,分别用来控制不同的电路模块。受MOSFET工作原理的影响,该电子积木只能用来控制直流电路,比如直流LED屏等,并不适合交流电路的控制。极限情况下该MOSFET开关可以用来控制100V/33A的直流电路,但控制的最小直流电压建议不低于9V。

在电路连接上,受控一端的连线稍微要麻烦一些。以控制12V的LED灯带为例,首先在正极(+)和负极(-)间连接好电源:

接着接LED灯带的正极连接到模块的正极(+)上,LED灯带的负极则连接到开关1(S1)上:

如果有其它的LED灯带需要控制,同样只需将LED灯带的正极连接到模块的正极(+),LED灯带的负极则依次连接到开关2(S2)、开关3(S3)、开关4(S4)上:

控制端的连线就简单多了,我们只需要通过传感器连接线,将相应的控制端口与Arduino的传感器扩展板连接起来,就可以通过Arduino来控制这些12V的LED灯带了。实验中我们接了两个LED灯带:

测试代码如下所示:

int s1Pin = 6;
int s2Pin = 7;

void setup() {
  pinMode(s1Pin, OUTPUT);
  pinMode(s2Pin, OUTPUT);
}

void loop() {
  int i;
  
  digitalWrite(s1Pin, HIGH);
  digitalWrite(s2Pin, HIGH);
  delay(500);

  digitalWrite(s1Pin, LOW);
  digitalWrite(s2Pin, LOW);
  delay(500);
  
  for (i = 0; i < 10; i ++) {
    digitalWrite(s1Pin, HIGH);
    delay(500);
    digitalWrite(s1Pin, LOW);
    delay(500);
  }
  
  for (i = 0; i < 100; i ++) {
    digitalWrite(s2Pin, HIGH);
    delay(50);
    digitalWrite(s2Pin, LOW);
    delay(50);
  }
}

下面是拍摄的视频效果:

No tags

SD卡是我们经常使用到的一种存储设备,像数码相机,MP3,阅读器,GPS导航仪等都会使用到它。在使用Arduino的时候,如何我们要将一些数据记录下来,就可以使用到SD卡。不过要让SD卡能够同Arduino配合使用可不是一件简单的事情,首先在硬件上我们要解决其与Arduino连接的问题,其次软件上我们还要解决文件的存储问题,以便使用读卡器之样的设备能够很方便地将保存在SD卡中的数据读出。

针对硬件上的需求,我们开发了SD卡模块,它通过SPI接口与Arduino进行通信:

同时,为了方便同Arduino的SPI接口进行连接,提供了相应的IDC扩展板,该扩展板上有一个专门为SPI设计的六芯插座:

电路的连接上则依然沿用了电子积木所一向强调的即插即用方式,我们只需要用6芯的IDC连接线,将IDC扩展板上的SPI插座与SD卡模块上的相应插座连接起来,就完成了电路部分的连接:

回到软件部分,我们的目标是要让SD卡上记录的数据能够在电脑上顺利地读出,因此在向SD卡上记录相应数据的时候需要遵循一定的规定,即文件系统的类型。对于像Arduino这样的小系统来讲,相对简单的FAT16是一个比较不错的选择。

在这里我们要用到读卡器来对SD卡进行相应的处理,将其格式化成FAT文件系统:



Arduino上使用的软件则是基于SparkFun提供的FAT库,但根据硬件差异做了一定的修改,你可以从这里下载到修改过的FAT库。该库需要被解压缩到Arduino的hardware\libraries子目录中。

测试时使用的时FAT库里自带的示例程序:

//Include all the libraries necessary for FAT32
#include <byteordering.h>
#include <fat.h>
#include <FAT16.h>
#include <fat_config.h>
#include <partition.h>
#include <partition_config.h>
#include <sd-reader_config.h>
#include <sd_raw.h>
#include <sd_raw_config.h>

FAT TestFile;      //This will be the file we manipulate in the sketch
char buffer[512];  //Data will be temporarily stored to this buffer before being written to the file
int read_size=0;   //Used as an indicator for how many characters are read from the file
int count=0;       //Miscellaneous variable

void setup()
{
  Serial.begin(9600);  //Initiate serial communication at 9600 bps

  TestFile.initialize();  //Initialize the SD card and the FAT file system.
  Serial.println("Starting...");
  TestFile.create_file("Sample.txt");  //Create a file on the SD card named "Read_File_Test.txt"
                                       //NOTE: This function will return a 0 value if it was unable to create the file.
  TestFile.open();  //Now that the file has been created, open it so we can write to it.
}

void loop()
{
  TestFile.write("This is test data.");  //using the write function will always write to the beginning of the file. 
                                         // Here we add some text to the file.
  TestFile.close();  //We are done writing to the file for now. Close it for later use.

  while(1){
    TestFile.open();  //Open the file. When the file is opened we will be looking at the beginning of the file.
    read_size=TestFile.read(buffer); //Read the contents of the file. This will only read the amount of data specified
                                    // by the size of 'buffer.'

    Serial.println(read_size, DEC);  //Print the number of characters read by the read function.
    for(int i=0; iSerial.print(buffer[i], BYTE);  //Print out the contents of the buffer.
    }
    Serial.println();

    sprintf(buffer, "%d", count++); //Now we'll use the buffer to write data back to the file. 
                                   // Here's we'll only add one value to buffer, the 'count' variable. 
    TestFile.write(buffer);         //Write the new buffer to the end of the file
    TestFile.close();               //Close the file for later use.

    delay(1000);  //Wait one second before repeating the loop.
  }
}

这样Arduino在运行的时候,会不断地向SD卡模块上的Sample.txt文件中写入相应的数据,随后我们同样可以通过读卡器读出该文件中记录的内容。

需要注意的是,由于用来操作SD卡和文件系统的代码相对较多,目前该模块只能运行在带有ATmega328P的Arduino上面。另外,目前市场上可选的SD卡种类繁多,可能不是每种SD卡都能够被很好的兼容,目前我测试过通过的SD卡有:

  • SanDisk 2GB SD2 Card
  • Apacer 60X 1GB SD Card

测试没有通过的SD卡有:

  • SanDisk 16MB SD Card

·

Older posts >>

Theme Design by devolux.nh2.me