Category Archives: 机器人小组

Connector Basics


Connectors are used to join subsections of circuits together. Usually, a connector is used where it may be desirable to disconnect the subsections at some future time: power inputs, peripheral connections, or boards which may need to be replaced.

Covered in This Tutorial

In this tutorial we will go over:

  • Basic connector terminology
  • Categorize connectors into distinguishable categories
  • Talk about the differences between connectors within those categories.
  • Show how to identify polarized connectors
  • Talk about which connectors are best suited for certain applications

Suggested Reading

You may find these concepts useful before starting on this tutorial:

Connector Terminology

Before we get started discussing some commonly used connectors, let’s explore the terminology used to describe connectors.

Gender – The gender of a connector refers to whether it plugs in or is plugged into and is typically male or female, respectively (kids, ask your parents for a more thorough explanation). Unfortunately, there are cases where a connector may be referred to as “male” when it would appear to be female; in the examples section, we’ll point a few of those out as we discuss individual component types and explain why that’s the case.

Male and female 2.0mm PH series JST connectors

Male (left) and female 2.0mm PH series JST connectors. In this case, gender is determined by the individual conductor.

Polarity – Most connectors can only be connected in one orientation. This trait is called polarity, and connectors which have some means to prevent them being connected wrong are said to be polarized, or sometimes keyed.

North America wall plug

A polarized North American wall plug. By having two different widths for the plug blades, the plug will only go into the outlet one way.

Contact – Contacts are the business portion of the connector. They are the metal parts which touch each other, forming an electrical connection. This is also where problems occur: the contacts can become soiled or oxidized, or the springiness required to hold the contacts together may fade with time.

ADH8066 mating connector

The contacts on this connector are clearly visible.

Pitch – Many connectors consist of an array of contacts in a repeated pattern. The pitch of the connector is the distance from the center of one contact to the center of the next. This is important, because there are many families of contacts which look very similar but may differ in pitch, making it difficult to know that you are purchasing the right mating connector.

.1" pin header connector examples

The pitch of the pins on the headers on a standard Arduino is .1″.

Mating cycles – Connectors have a finite life, and connecting and disconnecting them is what wears them out. Datasheets usually present that information in terms of mating cycles, and it varies widely from one technology to another. A USB connector may have a lifetime in the thousands or tens of thousands of cycles, while a board-to-board connector designed for use inside of consumer electronics may be limited to tens of cycles. It’s important that you select a connector with a suitable life for the application.

Connector for GS406 GPS module

Mating connector for the GS406 GPS module. The connector’s datasheet indicates a maximum of 50 insertion cycles for this part.

Mount – This one has the potential for being confusing. The term “mount” can refer to several things: how the connector is mounted in use (panel mount, free-hanging, board mount), what the angle of the connector is relative to its attachment (straight or right-angle), or how it is mechanically attached (solder tab, surface mount, through hole). We’ll discuss this more in the examples section for each individual connector.

Comparison of different mounting methods for barrel-type connectors

A comparison of three different methods of mounting the same barrel connector: (left to right) board mount, inline cable mount, and panel mount.

Strain relief – When a connector mounts to a board or cable, the electrical connections tend to be somewhat fragile. It is typical to provide some kind of strain relief to transfer any forces acting on that connector to a more mechanically sound object than the fragile electrical connections. Again, there will be some good examples of this later on.

1/8" Headphone jack showing strain relief

This 1/8″ headphone jack comes with a strain relief “boot” slid over the cable to prevent forces on the cable from being transmitted directly to the electrical joints.

USB Connectors

USB connectors come in two flavors: host and peripheral. In the USB standard, there is a difference between the two, and the connectors on cables and devices reflect this. However, all USB connectors will have some things in common:

  • Polarization – A USB connector can only nominally be inserted one way. It may be possible to force a connector in wrong, but that will result in damage to the device.
  • Four contacts – All USB connectors have at least four contacts (although some may have five, and USB 3.0connectors have even more). These are for power, ground, and two data lines (D+ and D-). USB connectors are designed to transmit 5V, up to 500mA.
  • Shielding – USB connectors are shielded, such that a metal shell which is not part of the electrical circuit is provided. This is important to keep the signal intact in environments with a lot of electrical “noise”.
  • Robust power connection – It’s important for the power pins to make connection before the data lines, to avoid trying to power the device over the data lines. All USB connectors are designed with this in mind.
  • Molded strain relief – All USB cables have plastic overmolding at the connector to prevent strain on the cable that could potentially damage the electrical connections.

Labeled image of USB extension cable

USB extension cable, with some of the common features of USB connectors labeled.

USB-A Connectors

USB-A female is the standard “host” connector type. This is found on computers, hubs, or any device intended to have peripherals plugged into it. It is also possible to find extension cables with a female A connector and a male A connector on the other end.

USB-A ports on a laptop computer.

Female USB-A ports on the side of a laptop. The blue connector is USB 3.0 compliant.

USB-A male is the standard “peripheral” connector type. Most USB cables will have one end terminating in a USB-A male connector, and many devices (such as keyboards and mice) will have a built-in cable terminated with a USB-A male connector. It’s also possible to find USB-A male connectors that are board mountable, for devices like USB memory sticks.

USB-A male connector examples

Two types of Male USB-A connectors, on a SparkFun Cerberus cable and an AVR Stick development board.

USB-B Connectors

USB-B female is a standard for peripheral devices. It’s bulky, but robust, so in applications where size is not an issue, it’s the preferred means for providing a removable connector for USB connectivity. It is usually a through-hole board mount connector, for maximum reliability, but there are panel-mount options for it as well.

USB-B connector on an Arduino Uno

Arduino boards, including this Uno, have long used the female USB-B connector, due to its low cost and durability.

USB-B male is almost exclusively found at the end of a cable. USB-B cables are ubiquitous and inexpensive, which also contributes to the popularity of the USB-B connection.

Male USB-B connector

USB-B male connector on the end of a SparkFun Cerberus cable.

USB-Mini Connectors

The USB-Mini connection was the first standard attempt to reduce the size of the USB connector for smaller devices. USB-Mini female is typically found on smaller peripherals (MP3 players, older cellphones, small external hard drives), and is usually a surface mount connector, trading robustness for size. USB-Mini is slowly being phased out in favor of the USB-Micro connector.

USB-Mini female connector

USB-Mini female connector on a Protosnap Pro Mini.

USB-Mini male is another cable-only connector. As with USB-B, it’s extremely common, and cables can be found cheaply almost anywhere.

USB-Mini male connector

USB-Mini male connector on the end of a SparkFun Cerberus cable.

USB-Micro Connectors

USB-Micro is a fairly recent addition to the USB connector family. As with USB-Mini, the primary concern is size reduction, but USB-Micro adds a fifth pin for low-speed signalling, allowing it to be used in USB-OTG (On-the-go) applications where a device may want to operate as either a host or a peripheral depending on circumstances.

USB-Micro female is found on many newer peripherals, such as digital cameras and MP3 players. The adoption of USB-micro as a standard charge port for all new cellular phones and tablet computers means that chargers and data cables are becoming increasingly common, and USB-Micro is likely to supplant USB-Mini in the coming years as the small-factor USB connector of choice.

USB-Micro female connector

USB-Micro female connector on a LilyPad Arduino USB board.

USB-Micro male is also a cable-only connector. There are generally two types of cables with USB-Micro male ends: one for connecting a device with a USB-Micro port as a peripheral to a USB host device and one for adapting the USB-Micro female port to a USB-A female port, to be used in USB-OTG capable devices.

USB-Micro male connector

USB-Micro male connector on the SparkFun Cerberus cable.

USB-A female to USB-Micro adapter

Adapter pigtail for using USB-OTG capable devices having only a USB-Micro port with standard USB peripherals. Note that not all devices supporting USB-OTG will work with this pigtail.

Audio Connectors

Another familiar connector group are those used for audio-visual applications–RCA and phono. While these can’t truly be considered to be of the same family, as the various USB connectors are, we’ll consider both of them to be in the same vein.

“Phone” Type Connectors

You’ll probably immediately recognize the 1/8″ version of this connector as a the plug on the end of a pair of headphones. These connectors actually come in three common sizes: ¼” (6.35mm), 1/8″ (3.5mm), and 2.5mm. ¼” size connectors find a lot of use in the professional audio and music community- most electric guitars and amplifiers have ¼” tip-sleeve (TS) jacks on them. 1/8″ tip-ring-sleeve (TRS) is very common as the connector for headphones or audio output signals on MP3 players or computers. Some cell phones will provide a 2.5mm tip-ring-ring-sleeve (TRRS) jack for connecting to headphones that also include a microphone for hands-free communications.

The common availability of these connectors and cables makes them a good candidate for general purpose connectivity applications–for instance, long before USB, Texas Instruments graphing calculators used a 2.5mm TRS connector for a serial programming connector. It should be remembered that tip-sleeve connector types are not designed for carrying power; during insertion, the tip and the sleeve can be momentarily shorted together, which may damage the power supply. The lack of shielding makes them poor candidates for high-speed data, but low speed serial data can be passed through these connectors.

1/8" TRS phone plug

Headphone-type TRS phone plug, 1/8″. Typically, tip and ring will carry the stereo audio signals while sleeve will be connected to ground.

1/8" TS phone plug

1/8″ phone plug. Note the lack of a ring contact on this connector.

1/8" board mount headphone jack

1/8″ board mount headphone jack with pins corresponding pin connections labeled. When no jack is inserted, an internal switch connects the tip and ring pins to the adjacent unmarked pins, allowing insertion detection.

RCA Connectors

Familiar as the home-stereo connector of choice for many decades, the RCA connector was introduced in the 1940s by RCA for home phonographs. It is slowly being supplanted by connections like HDMI in the audio-visual realm, but the ubiquity of the connectors and cables makes it a good candidate for home-built systems. It will be a long time before it is obsolete.

Female RCA connectors are usually found on devices, although it is possible to find extension or conversion cables with female jacks on them. Most RCA connectors are connected to one of four types of signals: component video (PAL or NTSC, depending on where the equipment was sold), composite video, stereo audio, or S/PDIF audio.

Female RCA plug, for video signals.

Female RCA connector, for video signals. Typically, NTSC or PAL video signal connectors will be yellow.

Male RCA connectors are usually found on cables.

Male RCA plugs

Male RCA plugs. Red and white are usually for audio applications, with red denoting the “right” audio channel.

Power Connectors

While many connectors carry power in addition to data, some connectors are used specifically to provide power connections to devices. These vary widely by application and size, but we will only focus on some of the most common ones here.

Barrel Connectors

Barrel connectors are typically found on low-cost consumer electronics which can be plugged into wall power via bulky AC wall adaptors. Wall adaptors are widely available, in a variety of power ratings and voltages, making barrel connectors a common means for connecting power to small projects.

The female barrel connector, or “jack”, can be purchased in several varieties: PCB mounted (surface mount or through hole), cable mount, or panel mount. Some of these connectors will have an additional contact that allows the application to detect whether a power supply is plugged into the barrel jack or not, thus allowing the device to bypass batteries and save battery life when running on external power.

Female barrel connector

Female barrel connector. When no plug is inserted, the “insertion detection” pin will be shorted to the “sleeve” pin.

The male barrel connector, or “plug”, is usually only found in a wire termination variety, although there are multiple methods of attaching the plug to the end of the wire. It’s also possible to get plugs that come pre-attached to a cable.

Male barrel plug

Unattached male barrel plug, for attachment to any power supply. Note that the sleeve connection is designed to be crimped onto the wire for extra strain relief.

Barrel connectors provide only two connections, frequently referred to as “pin” or “tip” and “sleeve”. When ordering, there are three differentiating characteristics of a barrel connection- inner diameter (the diameter of the pin inside the jack), outer diameter (the diameter of the sleeve on the outside of the plug), and polarity (whether the sleeve voltage is higher or lower than the tip voltage).

Sleeve diameter is most commonly either 5.5mm or 3.5mm.

Pin diameter is contingent upon sleeve diameter; a 5.5mm sleeve will have either a 2.5mm or 2.1mm pin. Unfortunately, this means that a plug designed for a 2.5mm pin will fit in a 2.1mm jack, but that the connection will be, at best, intermittent. 3.5mm sleeve plugs usually mate to a jack with a 1.3mm pin.

Polarity is the final aspect to consider; most often, the sleeve will be considered 0V and the tip will be a positive voltage relative to the sleeve. Many devices will have a small diagram indicating the polarity expected by the device; care should be taken to adhere to this, as an improper power supply may damage the device.

Plugs of both sleeve sizes are usually 9.5mm long, but longer and shorter ones do exist. All SparkFun products use a positive polarity 5.5mm sleeve and a 2.1mm pin; we recommend sticking to that standard where possible, as it seems to be the most common flavor found in the wild.

Barrel connector polarity label

Common polarity diagrams for AC adaptors with barrel plugs. Positive polarity (tip positive, sleeve 0V) is most common. Diagram courtesy Wikipedia user Three-quarter-ten.

“Molex” Connectors

Most computer hard drives, optical drives, and other internal peripherals get power through what is typically called a “Molex” connector. To be more accurate, it’s a Molex series 8981 connector–Molex is actually the name of the company which initially designed this connector back in the 1950s–but common usage has denuded that fact somewhat.

Molex connectors are designed to carry a lot of current: up to 11A per pin. For projects where a lot of power may be needed–a CNC machine, for instance, or a 3D printer- a very common method for powering the project is to use a desktop PC power supply and connecting the various system circuits through Molex connectors.

The Molex connector is one where the male/female terminology is a bit odd. The female connector is usually found on the end of a cable, and it slips inside of a plastic shell which surrounds the male pins on the male connector. Usually, the connectors are press-fit only, and very, very tight–they are intended to be connected and disconnected only a few times and, as such, are a bad choice for systems where connections will frequently be changed.

Male Molex connector

Male Molex connector. The gender of the pins inside the connector is what signifies the gender of the connector as a whole.

Female Molex connector

Female Molex connector on a project power supply.

IEC Connector

As with the Molex connector, this is a case where a generalized component name has come to be synonymous with a single, particular item. IEC connector usually refers to the power supply inlet which is commonly seen on desktop PC power supplies. Strictly speaking, that’s an IEC 60320-1 C13 (female) and C14 (male) connector.

IEC 60320-1 C14 male connector

C14 male IEC power inlet, on a DC project power supply. Note that, as with the Molex connector, the gender of the connector is defined by the pins within the hood.

C13 female IEC power connector

C13 female IEC power connector, on a fairly standard AC power supply cable. Cables with this end can be found all around the world, usually with the dominant local AC connector at the other end.

IEC connectors are used almost exclusively for AC power input. The nice thing about using one on a project is that IEC-to-wall cables are extremely common and available with localized wall plugs for most international locations!

JST Connector

At SparkFun, we frequently refer to “2.0mm JST Connectors”. This is yet another generalization of a specific product- JST is a Japanese company which makes high-quality connectors, and our 2.0mm JST connector of choice is the PH series two-position polarized connector.

All of SparkFun’s single-cell lithium-polymer ion batteries come standard with this type of JST connector, and many of our boards include this connector (or a footprint for it) as a power supply input. It has the advantage of being compact, durable, and difficult to connect backwards. Another feature, which can be an advantage or a disadvantage, depending on how you look at it, is that the JST connector is wicked hard to disconnect (although a carefully applied diagonal cuttercan be helpful!) once it’s mated. While this makes it unlikely to fail during use, it also means that disconnecting the battery for charging can damage the battery connector.

2-Pin JST male connector on a LilyPad Arduino USB board

2-Pin JST male connector on a LilyPad Arduino USB board. Again, as with the Molex, the pins inside the hood determine the gender of the connector.

Male and female 2-pin JST connectors

Male and female 2-pin JST connectors.

There are PH series connectors with more than two positions; SparkFun even sells them. However, our most frequent application is for the 2-position battery connection.

Pin Header Connectors

Pin header connectors comprise several different means of connection. Generally, one side is a series of pins which are soldered to a PCB, and they can either be at a right-angle to the PCB surface (usually called “straight”) or parallel to the board’s surface (confusingly referred to as “right-angle” pins). Such connectors come in a variety of pitches, and may have any number of individual rows of pins.

Right angle female header pin connector

Right-angle female header pin connection on an FTDI basic board.

The most commonly seen pin headers are .1″ single or double row connectors. These come in male and femaleversions, and are the connectors used to connect Arduino boards and shields together. Other pitches are not uncommon; for instance, the XBee wireless module uses a 2.0mm pitch version of the same connector.

.1" pin header connector examples

.1″ pin header connectors, male and female, on an Arduino Uno board.

A common variation on this part is a “machine pin” version. While the normal version is formed out of stamped and folded sheet metal, machine pin connectors are formed by tooling the metal into the desired shape. The result is a more robust connector, with a better joint and longer life, making it somewhat more expensive.

Female machine pin headers

Female machine pin headers. Note that these are designed to be snapped apart into smaller sections, while standard .1″ female header pin connectors are not. It’s also important to note that not all non-machine pin header connectors will mate with the machine pin variety.

Cables made to connect to these pin headers are usually one of two types: individual wires with crimp connectors on them or ribbon cables with insulation displacement connectors. These can simply be clamped onto the end of a ribbon cable, which creates a connection to each one of the conductors in the ribbon cable. Generally, cables are only available as female gender and expect a male pin to mate with.

Crimp connected header cable

Six-position crimp-type cable. Each wire is individually stripped, a connector crimped to it, and then the connectors are inserted into the plastic frame.

2x5 insulation displacement connectors on a ribbon cable

2×5 insulation displacement connectors (IDC) on a ribbon cable. This type of cable can be quickly assembled because it does not require stripping of individual connectors. It also has polarizing tabs on each end, to prevent incorrect insertion in the mating board-side connector.

Temporary connectors

Screw Terminals

In some cases, it may be desirable to be able to connect bare, unterminated wire to a circuit. Screw terminals provide a good solution for this. They are also good for situations in which a connection should be capable of supporting multiple different connecting devices.

The downside of screw terminals is that they can come undone fairly easily, leaving a bare wire waving around in your circuit. A small dab of hot glue can address this without being too difficult to remove later.

Screw terminals are typically designed for a narrow range of wire gauges, and wires that are too small can be as big a problem as wires that are too big. SparkFun carries two types of screw terminal–a .1″ pitch version and a 3.5mm version. Most screw terminals are highly modular, and they can easily be extended at the same pitch by simply connecting two or more smaller sections together.

3.5mm screw terminals

-> 3.5mm pitch screw terminals, showing the insertion point of the wire to be connected, the retention screw which holds the wire in place, and the modular connectors on the sides of the individual units allowing multiple pieces to be ganged together. <-

Banana Connector

Most pieces of power test equipment (multimeters, power supplies) have a very simple connector called a “banana jack” on it. These mate to “banana plugs”, crimped, sprung metal plugs, meant to make a single power connection. They are frequently available in a stackable configuration and can be easily connected to any type of wire. They are capable of carrying several amps of current and are inexpensive.

Banana plug

Stackable banana plug. Note that there are two different ways to plug in an additional banana plug.

Variable power supply with banana plugs

Extech variable bench supply with banana jacks on the front.

Alligator Clip

Named for obvious reasons, alligator clips are good for test connections to posts or bare wires. They tend to be bulky, easily cause shorts to nearby bare metal, and have a reasonably poor grip that can be easily compromised. They are primarily used for low-cost connections during debugging.

Alligator clips

“third hand” tool, which uses alligator clips to hold work pieces, holding a wire terminated with an alligator clip for electrical test. Note the plastic boot surrounding the alligator clip, to make it less likely to short to other connections.

IC Clip (or IC Hook)

For more delicate probing operations, there are a variety of IC clips on the market. These are sized to allow a user to clip them onto the pins of an IC without contacting adjacent pins; some of them are delicate enough to be clipped onto even fine-pitched SMD component legs. These smaller clips can be found on logic analyzers as well as test leads, which are great for prototyping or troubleshooting circuits.

Large IC clip

large IC clip on the end of a wire. This clip is still small enough to be connected to a single leg on a through-hole chip without causing a problem for adjacent pins.

Other Connectors

RJ-type Modular Connectors

Registered jack connectors are standard for telecommunications equipment into a local exchange. The names one normally hears associated with them (RJ45, RJ12, etc) are not necessarily correct, as the RJ designator is a based on a combination of the number of positions, the number of conductors actually present, and the wiring pattern. For example, while the ends of a standard ethernet cable are usually referred to as “RJ45”, RJ45 actually implies not only an 8 position, 8 conductor modular jack, it also implies that it is wired for ethernet.

These modular connectors can be very useful, since they combine ready availability, multiple conductors, moderate flexibility, low cost, and moderate current carrying capacity. While not originally intended to deliver a great deal of power, these cables can be used to deliver data and a couple of hundred milliamps from one device to another. Care should be taken to ensure that jacks provided for applications like this are not connected to conventional ethernet ports, as damage will result.

8p8c "RJ45" style modular jack

A standard 8p8c (8-position, 8-conductor) “RJ45” modular jack. Be aware that if you intend to use this type of jack to pass DC signals and power, you must avoid using connectors with built-in signal transformers.

D-sub Type Connectors

Named for the shape of their shell, D-subminiature connectors are a classic standard in the computing world. There are four very common varieties of this connector: DA-15, DB-25, DE-15, and DE-9. The pin number indicates the number of connections provided, and the letter combination indicates the size of the shell. Thus, DE-15 and DE-9 have the same shell size, but a different number of connections.

Female board-mount DE9 connector

Female DE-9 board-mount connector. Gender is defined by the pins or sockets associated with each signal, not the connector as a whole, making this connector female despite the fact that it effectively inserts into the shell of the mating connector.

DB-25 and DE-9 are the most useful to the hardware hacker; many desktop computers still include at least one DE-9 serial port, and often one DB-25 parallel port. Cables terminated with DE-9 and DB-25 connectors are widely available, too. As with the modular connector above, these can be used to provide power and point-to-point communications between two devices. Again, since the common usage of these cables does not include power transmission, it is very important that any repurposing of the cables be done cautiously, as a non-standard device plugged into a standard port can easily cause damage.

Resources and Going Further

You should now have a good idea which connectors are suited best for certain applications and which connectors will be useful to you in your next project. Please check out these other links to lean more about connectors.

  • Giant database of connectors and pinouts – Everything you never wanted to know about pretty much any connector, ever. A good site for basic info about connectors, but often quite shy on interface details.
  • Wikipedia article on registered jack connectors – More on the confusing world of the registered jack (RJ) connector, which is often misunderstood and misused.
  • Wikipedia article on D-subminiature connectors – As with the registered jack connector, misinformation abounds on the D-subminiature standard. Wikipedia has a great article about it.
  • Mouser electronics catalog – Browsing an electronics supplier catalog is often a great place to start looking for the name of an unidentified connector; Mouser’s enhanced online catalog is just as good as the print version, with fewer papercuts!

If you’d like top explore more SparkFun tutorials, check out these other offerings:



 Creative Commonstutorials are CC BY-SA 4.0

Original From:








图 矩频特性

步进电机的磁极数量规格和接线规格很多,为简化问题,我们这里就先只以四相步进电机为例进行讨论。所谓四相,就是说电机内部有4对磁极,此外还有一个公共端(COM)接电源, ABCD是四线的接头。而四相电机的可以向外引出六条接线(两条COM共同接入Vcc),即GNDABCD,也可以引出五条线,如图所示,所以有成为六线四相制和五线四相制。


                                    六线四相制                                              五线四相制
















  1. 直接利用晶体管来驱动,这需要你对电机和晶体管的详细参数有一定了解,才能选择恰当的参数去匹配他们。此外,还必须使用二极管来处理当电机内部线圈产生感应电动势逆向流入晶体管而对晶体管造成损害。

  1. 使用诸如ULN2003ULN2803这样的激励器,它实际是内部集成好了放大功能的集成电路芯片,此外也无需额外添加二极管,因为它已经内置了。

  2. 使用光耦,在驱动芯片或者晶体管的前端再加入光耦合器,以加强隔离步进电机的反电动势,以免损害Arduino



  1. 使用L293D这样的H桥的方式来驱动步进电机,详细请参考上两节介绍的L293 Motor Sheild官网的说明。





ULN2003采用的是达林顿管(Darlington transistor)方式来增强对大电流负载(如步进电机)的驱动,所谓达林顿管其实就是二级放大的三极管而已(如右图所示),经过恰当的三极管型号选择匹配后,两次放大的三极管驱动能力比一个三极管更强。详情请参考ULN2003DataSheet。但无论哪种方式,记住,使用额外的外接电源来驱动晶体管和集成芯片,它才是电机的真正的能量提供者。


前面所讲述的其实是一个简化模型,真正的步进电机步距角比较小。因为采用了图所示的多齿结构,这种结构类似于游标卡尺的工作原理,所以实际4相步进电机的步距角并非360°/8 = 45°。根据其规格书,本节范例所用的步进电机的步距角是5.625°,如果采用一二相励磁方式,则可以达到其一半的分辨率。





#include <Stepper.h>

const int stepsPerRevolution = 200;

Stepper myStepper(stepsPerRevolution, 8,9,10,11);            

int stepCount = 0;

void setup()

void loop()
  int sensorReading = analogRead(A0);
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  if (motorSpeed > 0)










Arduino UNO                                       x 1

旋转式电位器                                       x 4

自锁开关                                                x 1

MG995舵机                                           x 4

SG90舵机                                              x 1

5-9V电源                                                 —

一些线材                                                 —










        下载本文的附件,在淘宝搜索“亚克力加工 激光”搜索到相关店家,联系客服,选好合适的亚克力板材(3mm),发送dwg文件给客服,确认无误后下单即可。











IMG_20141006_203839 IMG_20141006_203854















Continue reading 【教程】Arduino控制的机械臂


一次性电池(Primary Battery)俗称“用完即弃”电池,因为它们的电量耗尽后,无法再充电使用,只能丢弃。常见的一次性电池包括:




可充电电池又称二次电池(Secondary battery)、蓄电池。可充电电池按制作材料和工艺上的不同,其优点是在充电后可多次循环使用,它们可全充放电两百多次甚至达2500次,充电电池的输出电流(负荷力)要比大部分一次性电池高。常见的类型有:


铅酸电池—电压约12V,能量密度较低,所以体积庞大。但可以做出大容量的、且能放出巨大电流的电池,所以仍然无法淘汰。汽车启动电机通常需要200A 左右的电流,目前只有铅酸电池能胜任。它还有一个好处:充电简单,不用放电完就可以充,而且是简单的恒压充电。过放电将导致电池性能下降,需要激活处理。体积较大的机器人可以考虑使用。



镍氢电池NiMH —电压约1.2V,有极轻微的记忆效应,容量较镍镉电池及碱性电池大,可充放电循环使用数百至二千几次。虽然镍氢电池的电压时1.2V,但在大多数场合可以替代1.5V的干电池。旧镍氢电池有较大的自放电,新的低自放电镍氢电池 自放电低至与碱性电池相约,已取代了镍镉电池和绝大部份碱性电池的用途,能量密度要求不高的场景下推荐使用。






锂聚合物电池 ( lithium polymer ) 或聚合物锂电池又可简称为锂聚电池 ( Li-Po )、或聚锂电池都是一种锂离子电池升级替代品。锂聚电池通常是由数个相同的平行子电池芯 ( secondary cells ) 来增加放电电流,或由数个电池包(pack)串联来增加可用电压。锂离子电池为锂聚电池的前身,主要差异为电解质使用液态有机溶液而非胶状或固态的聚合物,这使得锂聚合物电池在过压充电和短路时只会鼓起、不会发生爆炸的危险。现在电子产品(如智能手机、平板电脑、蓝牙耳机、非廉价移动充)中已有替代锂离子电池的趋势。
2、在能量密度没有特殊要求的场合,优先考虑镍氢电池NiMH,能量密度要求较高的场合(如四轴飞行器),优先考虑锂聚合物电池( Li-Po )。因为锂离子电池Li-ion虽然能量密度大,但意外短路或者充电方式不正确(这通常是由劣质充电器造成)都可能引发爆炸,威胁人身安全。

battary09 battary11

18650锂电短路爆炸后                                锂电过压充电爆炸试验












增量型编码器精度取决于机械和电气两种因素,这些因素有:光栅分度误差、光盘偏心、轴承偏心、电子读数装置引入的误差以及光学部分的不精确性。确定编码器精度的测量单位是电气上的度数,编码器精度决定了编码器产生的脉冲分度。以下用360°电气度数来表示机械轴的转动,而轴的转动必须是一个完整的周期。要知道多少机械角度相当于电气上的360度,可以用下列公式来计算:电气360 =机械360°/n°脉冲/转




/* 编码器示例程序
 * 源码作者相关信息
 * 该代码位于公共域


Encoder myEnc(5, 6);

void setup() {
  Serial.println("Basic Encoder Test:");

long oldPosition  = -999;

void loop() {
  long newPosition =;
  if (newPosition != oldPosition) {
    oldPosition = newPosition;


很多机器人竞技及爱好者制作过程中都需要能辨别颜色的传感器,比如赛道中的区域和信标的识别,又或者一些爱好者希望制作一款能够自动还原实物魔方(Rubik’s Cube)的机器人。


光的三原色(RGB) 颜料的三原色(CMYK)
色彩有减法,是由于物体表面上的颜料,吸收了日光中一部份的光波,反射日光其他的色光,当两种或多种颜料混合的时候,有更多的色光被吸收,越少的色光被反射,因而形成暗色或黑色。色彩的减片法是运用在颜料的混合,亦广泛地运用在印刷技术之中。同时色彩的减法又称为CMYK,CMYK分别代表三原色中彩蓝C (Cyan),洋红(Magenta),黄(Yellow),以及黑(Black)。黑色虽然不属于三原色的一种,但在印刷上,要加上黑颜料才能调出真正的黑色。
由上面的三原色感应原理可知,如果知道构成各种颜色的三原色的值,就能够知道所测试物体的颜色。对于 TCS3200来说,当选定一个颜色滤波器时,它只允许某种特定的原色通过,阻止其它原色的通过。例如:当选择红色滤波器时,入射光中只有红色可以通过,蓝色和绿色都被阻止,这样就可以得到红色光的光强;同理,选择其它的滤波器,就可以得到蓝色光和绿色光的光强。通过这三个值,就可以分析投射到 TCS3200 传感器上的光的颜色。仔细观察芯片的放大照片就可以看到。在透明塑料封装的芯片中央有一个硅片,其中的电极用纯金跳线和外部引脚相连。因为此芯片需要透光工作,不同于其它黑色塑料封装的芯片,恰好能够一窥集成电路IC芯片的内部结构。这是笔者故意设此一节讲解该芯片的原因之一。


Texas Advanced Optoelectronic Solutions(TAOS)公司是全球公认的光传感技术创新厂商,其所生产的TCS3200颜色识别芯片在机器人爱好者中得到广泛的应用。许多开源硬件模块提供商利用这块芯片制成颜色模块出售。这些模块大同小异,在使用前比较重要的还是了解此款芯片的基本工作原理。在AllDataSheet网站中中查找此款芯片的资料,可以了解到以下几点:






const int s0 = 8;
const int s1 = 9;
const int s2 = 12;
const int s3 = 11;
const int out = 10;
int pinRed = 2;
int pinGreen = 3;
int pinBlue = 4;
int red = 0;
int green = 0;
int blue = 0;
void setup()   
  pinMode(s0, OUTPUT);  
  pinMode(s1, OUTPUT);  
  pinMode(s2, OUTPUT);  
  pinMode(s3, OUTPUT);  
  pinMode(out, INPUT);  
  pinMode(pinRed, OUTPUT);  
  pinMode(pinGreen, OUTPUT);  
  pinMode(pinBlue, OUTPUT);  
  digitalWrite(s0, HIGH);  
  digitalWrite(s1, HIGH);  
void loop() 
  Serial.print(red, DEC);  
  Serial.print(green, DEC);  
  Serial.print(blue, DEC);  

  if (red < blue && red < green && red > 50)  
   digitalWrite(pinRed, HIGH); //点亮红色LED 
   digitalWrite(pinGreen, LOW);  
   digitalWrite(pinBlue, LOW);  

  else if (blue < red && blue < green)   
   digitalWrite(pinRed, LOW);  
   digitalWrite(pinGreen, LOW);  
   digitalWrite(pinBlue, HIGH); //点亮绿色LED 

  else if (green < red && green < blue)  
   digitalWrite(pinRed, LOW);  
   digitalWrite(pinGreen, HIGH); //点亮蓝色LED 
   digitalWrite(pinBlue, LOW);  

  digitalWrite(pinRed, LOW);  
  digitalWrite(pinGreen, LOW);  
  digitalWrite(pinBlue, LOW);  
void color()  
  digitalWrite(s2, LOW);  
  digitalWrite(s3, LOW);  
  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
  digitalWrite(s3, HIGH);  
  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  
  digitalWrite(s2, HIGH);  
  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);  




图所示为一直流电机(DC Motor)工作原理示意图。一对静止的磁极N和S之间,安装了一个可以绕中心轴旋转的漆包线线圈及其包裹的层叠硅钢片。硅钢片的目的是为了增强线圈产生的磁导,减少漏磁,而硅钢片叠加在一起相互又绝缘,是为了防止硅钢片内产生较大的涡流而损耗电能在发热上。中间转动的部分通常称为电枢,而线圈两端分别接在换向器的两个半圆铜片上,铜片之间相互绝缘,但分别接直流电源正负极。其中的磁场和产生的电枢转矩如何计算,为何需要设置换向器等问题在初中物理教科书中已有论述,在此不再赘述。


针对不同电机(或者大负载),需要使用不同型号的晶体管放大,所以读取电机铭牌上的参数就显得尤为重要。下面是一款常见电机的说明书部分截取,资料来自Sparkfun官网,电机型号为Micro Metal Gearmotor 30:1 ROB-08911,从中我们可以获得相关信息,从而达到合理选用匹配的驱动器(三极管)。


S9011 S9012 S9013 S9014 S9015 S9018 A1015 C1815
A42 A92 2N5401 2N5551 A733
C945 S8050 S8550 2N3906 2N3904





/* -----------------------------------------------------------
 * | Arduino小电机驱动电路示例程序 |
 * | 代码出处 |
 * | 连线时请注意参考右图引脚位置 |
 * -----------------------------------------------------------

int motorPin = 9; //定义电机连接引脚

void setup()
  pinMode(motorPin, OUTPUT);

void loop()

void motorOnThenOff(){
  int onTime = 2500; //调节开关占空比,也可使用PWM模拟输出方式
  int offTime = 1000;

  digitalWrite(motorPin, HIGH);
  digitalWrite(motorPin, LOW);

void motorOnThenOffWithSpeed(){

  int onSpeed = 200;
  int onTime = 2500;

  int offSpeed = 50;
  int offTime = 1000;

  analogWrite(motorPin, onSpeed);
  analogWrite(motorPin, offSpeed);

void motorAcceleration(){
  int delayTime = 50; //调整速度的时间间隔

  for(int i = 0; i < 256; i++){
    analogWrite(motorPin, i);

  for(int i = 255; i >= 0; i--){
    analogWrite(motorPin, i);


DCmotor08意法半导体公司(STMicroelectronics)生产的L293D和L298P两款芯片是市面上常见的H桥电机驱动集成芯片,它们的外形虽然看起来很不一样,但是只要掌握其中一种的原理和使用方法,再学习使用另外一个就不会太难。这两款芯片的细节参数请读者自行查阅AllDataSheet网站,但很多机器人或电子模块提供商已经将H桥做成了Arduino的专用模块甚至是盾牌(Sheild),配合专门的Arduino类库,使用起来会大为方便。本节以美国开源硬件商Adafruit制作的一款293 Motor Sheild为例来说明如何实现电机的调速与正反转。

此款Motor Sheild采用了两个L293及一个74HC595芯片,可以同时控制4路直流电机或者两路步进电机和舵机。本节仅介绍此款Motor Sheild普通直流电机的使用,关于步进电机和舵机,请参考本书后续部分或Adafruit官网:

此款Motor Sheild使用前有专配类库,可以到GitHub网站下载
需要注意的是,电机有四个连接口,如图所示,连接其中一个即可(下面的示例程序使用的是Motor #4)。此种连接端子用于连接电流较大的场合,故使用的是螺丝刀拧紧的方式,在把电线剥开一小段距离后,松开螺丝放入电线裸露端再拧紧螺丝即可。如果在使用过程中发现电机转向与你所需的方向相反,断开电源后将两根连接线交换位置后重新连接即可。

// Adafruit Motor shield library
// 此代码位于公共域

#include <AFMotor.h>
AF_DCMotor motor(4);

void setup() {
  Serial.println("Motor test!");

void loop() {
  uint8_t i;
  for (i=0; i<255; i++) {
  for (i=255; i!=0; i--) {
  for (i=0; i<255; i++) {
  for (i=255; i!=0; i--) {




图 舵机外观及内部主要结构示意图


图 舵机控制原理示意图




舵机除电源外,只要一根信号线即可;使用PPM(脉冲比例调制)信号控制;所谓“PPM”,是一个周期约20ms,其间有个宽度在2ms 左右的脉冲控制信号。一般是以1.5ms 为基准,此时舵机居中,小于1.5ms 舵机左转,大于1.5ms,舵机右转;至于角度和脉冲宽度关系各个产品不同,例如:0.5ms 对应左转90 度,2.5ms 对应右转90 度。
舵机内部实际上是由小电机、减速齿轮、驱动电路、位置反馈、比较电路等组成的闭环控制单元,由于其内置减速齿轮,所以输出力矩较大。最早将其改造为轮式机器人动力源的爱好者估计就看上了这些,他们将舵机的限位去除,位置反馈去除,舵机控制电路因得不到反馈,以为还未转到指定的角度,只好一直驱动电机转动,由于去除了限位,输出轴实现了连续转动,就成了一个减速直流电机。而且,利用其原来的左、右转控制逻辑,实现了正、反转控制。从本质上说,舵机和直流减速电机相同,只是利用了舵机内部的驱动和控制电路,从而简化了控制接口和电路。实际上:改造为连续转动的舵机 = 电调 + 直流减速电机电子调速器的控制信号和舵机一样,只是在小型电机中一般用不着电调,因为其功率不太大,一般在10A 以上,100A 也不足为奇,大材小用了。舵机还有一个优点是安装方便,除自身安装外,安装车轮也很方便,因为舵机一般提供丰富的轴输用舵机作为小车动力,可用MCU 直接驱动,不需要再设计、制作额外的驱动电路,这点对于DIY 者而言还是颇具吸引力的。而且控制所需的MCU 资源也有限,只要一个数字I/O口、一个定时器即可,虽说准确生成2ms 左右的脉冲有些技巧,但总的来说编程相对容易。如果用Arduino 系列控制器,其Servo 函数可让你一条语句“搞定”。
结论:使用舵机作为小车驱动一是为了便于装,二是为了便于控,三是可简化电路。其代价就是“力气”略小,可供选择的规格有限。还有就是略有些不“经济”,因为舵机的“贵贱”主要体现在其控制精度上,改为连续运转的驱动电机后,这部分功能给“废”了,是不是有些可惜?不过也不完全“浪费”,舵机的这部分控制通常采用PID 调节,其比例功能可用于调速,当输入的脉冲偏离中点(1.5ms)越远,其驱动电机的速度越快,而且其调节电路使用了电机反电势作为反馈,原设计大概是为了保证舵机在轻、重负荷下都可以按照相近的速度转到指定的角度,以实现较好的控制特性(比如说60 度/0.22 秒)。改为连续转动后正好作为调速功能,这也是使用舵机的一大优点。

图 连续舵机外观及中位点调节示意图


// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <> 

Servo myservo;  // create servo object to control a servo 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
void setup() 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
void loop() 
  val = analogRead(potpin);        // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);   // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);             // sets the servo position according to the scaled value 
  delay(15);                    // waits for the servo to get there 

// Sweep
// by BARRAGAN <> 
// This example code is in the public domain.

Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
int pos = 0;    // variable to store the servo position 
void setup() 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
void loop() 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees    {                                  // in steps of 1 degree      myservo.write(pos);              // tell servo to go to position in variable 'pos'      delay(15);                       // waits 15ms for the servo to reach the position    }    for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 



超声波发射器向某一方向发射超声波,在发射的同时开始计时,超声波在空气中传播,途中碰到障碍物就立即返回,超声波接收器收到反射波就立即停止计时。超声波在空气中的传播速度为340 m/ s,根据计时器记录的时间t,就可以计算出发射点距障碍物的距离S ,即:S = 340t / 2。超声波指向性强,在介质中传播的距离较远,利用超声波检测往往比较迅速、方便、计算简单、易于做到实时控制,并且在测量精度方面能达到工业实用的要求,因此在移动机器人的研制上也得到了广泛的应用。
压电式超声波发生器实际上是利用压电晶体的谐振来工作的,其外观结构与内部结构分别如图所示.该传感器有两个压电晶片和一个共振板, 当其两极外加脉冲信号,且频率等于压电晶片的固有振荡频率时,压电晶片将会发生共振,并带动共振板振动产生超声波。反之,如果两电极间未外加电压,当共振板接收到超声波时,将迫使压电晶片振动,将机械能转换为电信号,这时它就成为超声波接收器了。

图 超声波测距模块内部结构简图


声速 = 331 + 0.6 t (攝氏温度)




#define TRIGGER_PIN  12
#define ECHO_PIN     13

Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

void setup()

void loop()
  float cmMsec, inMsec;
  long microsec = ultrasonic.timing();

  cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
  //inMsec = ultrasonic.convert(microsec, Ultrasonic::IN);
  Serial.print("MS: ");
  Serial.print(", CM: ");
  //Serial.print(", IN: ");


图表 1
图 超声波测距模块对圆柱障碍物的测量值分布图

图3.12 超声波测距模块对平板障碍物的测量值分布图

图 超声波测距模块产生幻影原理示意及响应模糊算法对于复杂障碍物群的壁障轨迹


Hydraulic machines are machinery and tools that use liquid fluid power to do simple work. Heavy equipment is a common example.

In this type of machine, hydraulic fluid is transmitted throughout the machine to various hydraulic motors and hydraulic cylinders and which becomes pressurised according to the resistance present. The fluid is controlled directly or automatically by control valves and distributed through hoses and tubes.

The popularity of hydraulic machinery is due to the very large amount of power that can be transferred through small tubes and flexible hoses, and the high power density and wide array of actuators that can make use of this power.

Hydraulic machinery is operated by the use of hydraulics, where a liquid is the powering medium.

————————From wikipedia











Hydraulic pump

Hydraulic pumps supply fluid to the components in the system. Pressure in the system develops in reaction to the load. Hence, a pump rated for 5,000 psi is capable of maintaining flow against a load of 5,000 psi.

Pumps have a power density about ten times greater than an electric motor (by volume). They are powered by an electric motor or an engine, connected through gears, belts, or a flexible elastomeric coupling to reduce vibration.

Common types of hydraulic pumps to hydraulic machinery applications are;

An exploded view of an external gear pump.

  • Gear pump: cheap, durable (especially in g-rotor form), simple. Less efficient, because they are constant (fixed) displacement, and mainly suitable for pressures below 20 MPa (3000 psi).
  • Vane pump: cheap and simple, reliable. Good for higher-flow low-pressure output.
  • Axial piston pump: many designed with a variable displacement mechanism, to vary output flow for automatic control of pressure. There are various axial piston pump designs, including swashplate (sometimes referred to as a valveplate pump) and checkball (sometimes referred to as a wobble plate pump). The most common is the swashplate pump. A variable-angle swashplate causes the pistons to reciprocate a greater or lesser distance per rotation, allowing output flow rate and pressure to be varied (greater displacement angle causes higher flow rate, lower pressure, and vice versa).
  • Radial piston pump: normally used for very high pressure at small flows.

Piston pumps are more expensive than gear or vane pumps, but provide longer life operating at higher pressure, with difficult fluids and longer continuous duty cycles. Piston pumps make up one half of a hydrostatic transmission.



Control valves

Directional control valves route the fluid to the desired actuator. They usually consist of a spool inside a cast iron or steel housing. The spool slides to different positions in the housing, and intersecting grooves and channels route the fluid based on the spool’s position.

The spool has a central (neutral) position maintained with springs; in this position the supply fluid is blocked, or returned to tank. Sliding the spool to one side routes the hydraulic fluid to an actuator and provides a return path from the actuator to tank. When the spool is moved to the opposite direction the supply and return paths are switched. When the spool is allowed to return to neutral (center) position the actuator fluid paths are blocked, locking it in position.

Directional control valves are usually designed to be stackable, with one valve for each hydraulic cylinder, and one fluid input supplying all the valves in the stack.

Tolerances are very tight in order to handle the high pressure and avoid leaking, spools typically have a clearance with the housing of less than a thousandth of an inch (25 µm). The valve block will be mounted to the machine’s frame with a three point pattern to avoid distorting the valve block and jamming the valve’s sensitive components.

The spool position may be actuated by mechanical levers, hydraulic pilot pressure, or solenoids which push the spool left or right. A seal allows part of the spool to protrude outside the housing, where it is accessible to the actuator.

The main valve block is usually a stack of off the shelf directional control valves chosen by flow capacity and performance. Some valves are designed to be proportional (flow rate proportional to valve position), while others may be simply on-off. The control valve is one of the most expensive and sensitive parts of a hydraulic circuit.