Posts by Boomer48

Old dog still learning new tricks.

Servo Control Stuff

In the past few months I have been playing around with some cheap servos.  I started by making a four-leg walking “thingie” out of oversized Popsicle sticks.  The hardest part was keeping it balanced when one of the legs was lifted for movement.  That exercise prompted me to build a 3-DOF (degrees of freedom) tester so that I could test servo movement in general and also verify pulse width settings for the desired positions of each 3-DOF leg.  More recently I decided to buy a cheap 4-DOF robot arm kit that did not include control electronics.  This article is sort of a mishmash of what I’ve learned so far about servo control and includes schematics and PIC software for the 3-DOF tester and the 4-DOF robot arm controller.

Cheap Servos

The cheap servos available from online suppliers have a rotation range of 180 degrees.  They consist of a small motor, some gears, and a control circuit that requires a PWM (pulse width modulation) input.  In general, the PWM frequency is set to 50-Hz for servos and the servo position is varied by sending a pulse width of about 500 microseconds to about 2.5 milliseconds (1.5 milliseconds centers the servo).  The first information I found online indicated a range of 1ms to 2ms but that only moved the servos 45 degrees either side of center.

Servos1

PWM

One of my earlier projects (posted on my website) talked in detail about PWM so I won’t repeat that information here.  Check out the Model Train Controller for those details.  In my previous projects I used the PWM capability built into one of the PIC microcontrollers but that is insufficient when you have more than one servo to control.  There are a variety of ways to handle multiple servos and that includes external modules that do the PWM for you.  When I built the four-leg walker I used three servos per leg so I needed to control 12 altogether.  Fortunately, there is a module that accepts I2C serial command inputs to generate up to 16 separate PWM outputs.    Just search for “PCA9685” on ebay.  There are also some simple PWM modules that have either an LED or LCD and can output 2 or 3 separate PWM signals.  Mostly they are for manual control via the onboard buttons but they can also be controlled using a 9600 baud RS-232 type of interface.  I don’t recommend these for servo control because multiple PWM outputs can be generated in software for a PIC.  I used the PIC 16F688 chip for both the 3-DOF servo tester and the 4-DOF robot arm controller.

Software Generated PWM

As I indicated earlier, a typical PWM frequency for servos is 50-Hz and the servos are positioned by sending a pulse during each frequency period.  The 50-Hz interval is set in the PIC software using Timer0.  When it times out, an interrupt is generated and a flag is set to let the main routine know that it is time to send pulses to the servos.  Given that 50-Hz is a 20ms period it is easy to fit control pulses for several servos within each time frame.  For simplicity, the pulses for each servo are sent out sequentially.  That means that the PIC only has to use a single timer to generate the desired pulse widths.  For instance, Timer1 is loaded with the desired pulse time for servo1, the control output for servo1 is set high, the software waits for the timeout, and then the control output for servo1 is set low.  The sequence is repeated for each of the remaining servos.  The maximum pulse width for each servo is 2.5ms so four servos only use about half of each 20ms period.  The servos don’t care where the pulse occurs within the time frame.

3-DOF Servo Tester

20180531_163208

The servo tester uses potentiometers to allow variance of the servo pulse widths.  There is also a switch for forcing all servos to the center position.  An LCD is included so that the actual pulse width values can be observed without having to connect to an oscilloscope.  Each potentiometer forms a simple voltage divider between +5 volts and ground and is connected to a PIC pin that is set for use as an ADC (analog-to-digital conversion) input.  Based on the voltage read, a calculation is performed to convert the voltage to a servo pulse width.  Basically, the formula is: (V * 7.5) + 600.  I found that 600 was the “real” minimum pulse width for my servos as opposed to the stated value of 500.  Remember that “V” in the formula is actually the truncated 8-bit ADC value (0-255) of the input voltage from the potentiometer.  On its own, the PIC doesn’t do multiplies or divides so the simple method for getting 7.5 was to left shift the ADC value three times (multiply by 8) and then subtract the right shift of the ADC value (0.5).

The LCD interface uses just 3 PIC pins but requires the addition of a 74HC164 shift register.  The details of the hardware are discussed in the “3-wire, 8-bit LCD Interface” project on my website.  The LCD shown in the picture is an oversized, single line one that I picked up as surplus but the software will work with the standard 1602 LCD.

Servo Tester 3DOF

Servo tester 3DOF

4-DOF Robot Arm Controller

20180531_163721

20180531_163700-e1527886885312.jpg

The kit for the robot arm shown in the picture can be bought for $25-$30 online.  I opted for one with servos that have metal gears so it was at the high end of that range.  I attached the flat robot arm base to a large project box which contains the control circuitry.  The hardware/software was base lined off of the 3-DOF servo tester.  A fourth potentiometer was added to the hardware, limit checks were added to the software, and the routines for the LCD were stripped out.  I used the 3-DOF servo tester to get a feel for the maximum ranges I wanted for each servo.  The range checks are not essential but I thought they might be a good idea given that I let my young grandkids play with it.  I’ve debated adding simple Bluetooth control for the robot arm so I specifically left the PIC serial ports pins available.  Otherwise, the hardware/software could be modified for 5-DOF.  You might think that you could go to 6-DOF with 12 I/O lines but RA3 is an input only and digital only pin.

Robot Arm1

RobotArmCtrl1

Check out my other projects at: http://www.boomerrules.com

 

Turkeys

Since we’re so close to Thanksgiving, today I want to talk to you about turkeys.  Wild turkeys, that is, not the kind of turkeys we eat at Thanksgiving.  Did you know that the Pilgrims actually brought some turkeys with them on the boat?  Anyway, my interest in turkeys is just part of my interest in wildlife.  I’ve always been a city boy and I still get excited when I see things like deer and turkeys roaming around in the wild.  Snakes excite me too but not in a good way.

 

Unlike domesticated turkeys, wild turkeys are smart.  They are also very sociable.  They hang around in groups.  They play together.  And the older ones teach the younger ones everything they need to know about being a turkey.

 

When I lived in Iowa, I would drive by a small corn field on the way to church and every Fall there would be at least one bunch of turkeys grazing on corn that had fallen to the ground during the harvest.  In West Texas, though, the turkeys don’t have the luxury of corn fields.  I guess I don’t really know what they normally eat, but I do know that they like acorns.  I’ve seen a group of turkeys gathered around a tree eating acorns.  And when the supply of acorns runs low, one of the turkeys flies up into the tree and starts thrashing around to knock down more acorns.  See, I said they were pretty smart.  After awhile, the turkey will jump down from the tree to eat some acorns and another turkey will take its place in the tree.  It’s all part of living in community as turkeys.

 

So what do turkeys have to teach us about being a Christian?  Well, when Jesus founded the church, he basically told us to be like turkeys.  He didn’t say it exactly that way but he did expect us to be in community with one another.  He expected us to worship together.  He expected us to play together.  He expected us to teach each other how to be a Christian.  He expected us to take turns doing the work of “shaking the tree” so that everyone can get a share of the acorns.  And he expected us to be sociable enough that others would want to join our group.

 

Benjamin Franklin once wrote a letter to his daughter in which he compared the Bald Eagle and the turkey.  Basically, he said that the Bald Eagle looked good on the outside, but that it only cared about itself and was both a coward and a thief.  On the other hand, Ben praised the turkey for all of the things that I mentioned.  It’s like that for us as Christians too.  It doesn’t matter if you look more like a turkey than an eagle, it’s what’s inside that counts.  That’s what God sees in us and that’s what other people will experience through our actions.

Grandma

There are lots of stories about people who overcame some bad things in life but the one that I know best is the story of my grandmother.  When grandma was born in 1905 her father was in charge of running a logging camp.  Her mother took care of all of the cooking for the camp as well as taking care of her own family.  Grandma loved school but, when she was still in High School, her father died.  Her mother had to take over the day to day business of the logging camp management and that meant that grandma, as the oldest child, had to quit school and take over the cooking and looking after her younger brothers and sisters.  Grandma eventually found her way back to school and, when in her 80’s, grandma dressed in a cap and gown and proudly marched up to get her diploma.  Every one of her kids and most of her grandkids were there to cheer her on.

 

Grandma got married and had children.  She lost her youngest daughter as a baby and then, when grandma was 94, she lost her oldest daughter who was my mother.  In between, grandma and grandpa made a modest living until grandpa died when grandma was 61.  In order to pay the bills grandma worked fulltime as a housekeeper at the local hospital.  Eventually she worked her way up to be a nurses aide but not before she lead a difficult fight to get the housecleaning workers organized into a union.  Grandma was in her 70’s when she retired from fulltime work at the hospital but she continued doing volunteer work there for another 20 years because she just liked helping people.

 

Grandma got a little slower with age but she was still very active and mentally sharp into her late 90’s.  She collected family stories and pictures and put them into a family history.  When I would visit her she would tell me wonderful stories about how life was lived long ago and the interesting characters who were my ancestors.  Unfortunately, all of that came to a sudden end when grandma had a stroke at age 98.  The stroke caused grandma to lose her ability to walk and to talk and even to swallow.  After being so active and helpful she ended up bedridden, with a feeding tube, and only able to make sounds that couldn’t be understood as words.  But grandma was still there inside of that broken body and people say that her face lit up with joy when her many friends threw her a 100th birthday party.  Grandma had another stroke a few months later and was finally released from her earthly prison.

 

I read somewhere recently that life can only promise us pain so it’s up to us to create the joy.  When I think about that I’m reminded of grandma.  She endured what life threw at her and still managed to thrive and to be someone that made people feel good.  The Bible also tells us that we are certain to have suffering in our lives.  And, unfortunately, good people sometimes suffer more than bad people because life is not fair.  But the Bible also tells us that we can take joy and comfort in a life lived in Jesus.  And we can take joy and comfort in knowing that there is a better world after this one.  I know that grandma believed that.  And I think that’s what got her through the last year and a half of her life.

One Parade Too Late

He remembers watching the faces on T.V. as the tears began a salty cascade down their cheeks.  He cried as they cried, their impending separation ripping through more than twenty years of psychic dust and debris to lay open the feelings of a 19-year-old kid.  The feelings that came as he turned away from the girl he loved and marched across the tarmac toward the plane that would deliver him into the military and the uncertainty that surrounded the Viet Nam war.

He also remembers the faces on T.V. as they returned to an overwhelming display of yellow ribbons, smartly flapping flags, and old-fashioned victory parades.  Some of them reveled in the triumph of the moment, and some flushed with embarrassment, but all were thankful to be home.  The Gulf war was over and they were all treated as heroes.  And he cried again.

The year is 1971.  The 19-year-old kid is now 22 and he is sitting in the squadron commander’s office.  Quietly, but with a slight waver in his voice, he explains why his roommate has wandered off toward the ridge that overlooks the base.  A few months later the young man heads home before reporting for his next assignment.  Disappointment sets in when his young wife does not meet his plane.  It grows as he unlocks the door to her empty apartment.  It turns to crushing despair when he wakes the next morning and finds that he is still alone in bed.

This is one fragment of the era covered by what PBS describes as the “10,000-day War”.  It was a war that recorded tens of thousands of American names in black granite and altered the lives of so many others.  Those who answered the call of their country in the face of political divisions.  Those who, at worst, were reviled for their efforts and who, at best, were ignored.  Years later they shared, vicariously, in the celebration bestowed on the Gulf War veterans.  Ultimately, however, they realized that, for them, it was one parade too late.

Etekcity Wireless Socket Hacks

Outlets1

There are a zillion RF remote controlled outlets available but one of the most popular appears to be those from Etekcity.  I was able to pick up, at regular price, a set of five and two remote controls for less than $30 on Amazon.  I wasn’t sure what I was going to do with them but I figured it was a prime opportunity to do some hacking.

One thing to keep in mind is that these are only controllable with the included remote, and not via the internet.  But we will fix that.  Also, they are normally off when plugged in and go back to that state if power is lost.  I don’t know about you, but I have some applications where I want the outlet to be normally on instead.  We will fix that as well.  Just keep in mind that these hacks require some knowledge of electronics and basic soldering skills.

Making Normally On Outlets

Changing one of these outlets from normally off to normally on would seem to be a simple task because they use a pretty standard relay which should have pins for both states.  As it turns out the relay may or may not have the normally on pin, but it is not accessible on the circuit board.  That complicates our task but it’s probably a smart safety move by the manufacturer.  What that means, then, is that we need to find a way to reverse the on/off logic.

Unmodified

Cuts

LED Replaced

There are two pieces to reversing the logic.  The first is to change the polarity of the LED.  The solder pads for the LED are shown in the first picture.  Once the LED is removed, we need to make two cuts to the circuit traces as shown in the second picture.  The right hand cut separates the LED solder pad from ground.  We do that so that after the LED is reversed, we can solder that pad to +5 volts.  The left hand cut separates the base of the relay driver transistor from the 4700 ohm resistor.  That will allow the second logic polarity reversal to be installed.  Double check with an ohmmeter to make sure that the cuts are successful.  In the third picture we have reinstalled the LED with the anode now connected to the cut pad and to +5 volts.  The leads were just long enough on my unit so that I could bend it over to the +5 volt output of the 78L05 voltage regulator.

Transistor Added

The fourth picture shows the method used to reverse the logic for driving the relay.  I used a common 2N3904 NPN transistor (an equivalent would be ok) as an inverter.  The emitter is soldered to ground, the base is soldered to the on-board 4700 ohm resistor, and the collector is soldered to the base of the relay driver transistor.  In order to ensure that the relay driver transistor is normally switched on, I had to add a 4700 ohm resistor from its base to +5 volts.  Now, when the logic output is high, it will turn on the new transistor which will turn off the relay driver transistor.

Remote x-wired

If you want to take an extra step you can cross-wire the appropriate buttons in the remote so that the left button will turn on the modified outlet and the right button turn in off.  Basically you need to cut the circuit traces that go to the switch contacts that are closest to the middle of the board and then add jumper wires as shown in the picture

Internet Control

There are two methods possible for controlling the RF outlets from the Internet.  Both require the use of a cheap module like the ESP8266.  One method would be to wire into one of the remote controls and use a microcontroller to simulate the button presses.  The other less messy method is to use a microcontroller to take the place of the remote control.  That is what is described here.  The microcontroller will receive commands via the ESP8266, translate them into the proper RF bit pattern, and then send that bit pattern to an RF transmitter.  It sounds complicated but the only hard part is figuring out what the proper control codes are for your set of RF outlets.

There are many posts online that use an RF receiver and the audio input to a PC to figure out the codes.  I have the luxury of having a decent oscilloscope so it is easy for me to capture them.  I also have an RF sniffer circuit (detailed in one of my other electronics projects on my website) that allows me to capture RF transmissions using a terminal program on my PC.

Waveform

The frequency for communicating with the RF outlets is 433.92-MHz and the commands are comprised of a long sync bit, 24 data bits, and 1 stop bit.  The data encoding method used is On-Off-Keying (OOK) which means that data bits are differentiated by the on/off times.  There are no requirements in OOK for number of bits or period length.  That’s why there are so many variations out there for different devices.  I have seen that first hand by decoding security sensors and weather sensors.  The waveform looks similar to what is shown in the picture here.

Hardware

PIC WiFi Interface2

The schematic shown here is almost identical to the one I used in the one of the earlier Wi-Fi projects listed on my website.  The main difference is that the final version doesn’t have the USB interface but does have an interface to an RF transmitter module.  The transmitter module I used is labeled FS1000A and transmits at 433.92-MHz.  I have not tried other models of RF transmitters but most should work as long as they have similar characteristics.  The RF module is run from the +5 volt input and readily accepts the 3.3-volt logic level for the serial data bit stream from the PIC.

Some ESP8266 modules have their own 3.3 volt regulator onboard so the input to it would be 5 volts.  I have included a 3.3 volt regulator in my schematic for the PIC and it can also be used for the ESP module if it doesn’t have its own voltage regulator.  This allows the PIC and the ESP to communicate at the same logic levels without the need for converters.

You could simplify the ESP hardware by using the ESP-01 module and the adapter (shown here).  The adapter takes +5 volts and has an onboard 3.3 volt regulator.  If you go this route I also recommend that you buy the USB interface that is specifically made for the ESP-01.  It will make the setup of the ESP-01 much easier.

Software

The software listing is available below.  It is an extension of the software I wrote for a previous Wi-Fi project.  I chose that because I wanted to have the status response from the PIC displayed as simple graphics instead of text.  I also added code to output the single-pin serial bit stream to the RF transmitter.  Like the earlier version, I used HTML commands to draw circles that represent the status of each of the five remote switches.  Red=off, green=on, and white=unknown.  The important thing to remember is that there is no feedback from the remote switches themselves so the software can only maintain the status of the last command sent for each switch.  That means that every time there is a power up of the controller hardware the switch statuses are all unknown.  That’s it for this post.  Check out my other electronics projects at: http://www.boomerrules.com

ESP8266 RF Socket Controller

No Country for an Old Man

Strife in politics is nothing new but the division in this day and age has grown so hardened that there appears to be no common ground for compromise.  Terms that used to convey philosophical leanings have now taken on the status of derision and expletives.  I’m talking about words like “Conservative”, and “Liberal”.  We have become so polarized that it has literally become a “my team versus your team” mentality with no handshakes after each contest.  Worse yet is the fact that each side continues to push for more and more ideological purity, thus terms like “RINO”.

Some of us may think we have an open mind to other political stripes so let me provide a little test.  I will list the attributes of two individuals and you can decide how to categorize them.  The first is a white male in his late 60’s.  He is a military veteran, having served during the time of the Viet Nam war but not in the war.  He is most comfortable in jeans, tee shirts, and cheap sneakers, all purchased at Wal-Mart.  He owns several guns, mostly of the handgun variety.  He thinks there should be some limits on abortion.  He has always worked to live within his means and believes that the government should too.  He attends church on a regular basis.

The second individual is also a white male in his late 60’s.  He went to high school and college in Southern California during the 1960’s and 1970’s.  He holds three degrees in all – Psychology, Sociology, and Computer Science.  He was opposed to the Viet Nam war.  He believes that a woman should have the right to an abortion.  He believes that those who have much should help those who don’t.  He believes that there should be better screenings for gun ownership.  He thinks that those who believe that the Universe came about exactly as it is stated in the Bible are intentionally ignorant of scientific facts.

So, what do you think?  Number 1 sure sounds like a “Conservative” and number 2 sure sounds like a “Liberal”, don’t they?  At this point, however, many of you may suspect that this is a trick and you would be right.  Both descriptions fit the same individual.  Before you write this individual off as some sort of Schizophrenic oddity let me assure you that he is a happily married (47 years), middle-class individual who is well within the norms of sanity.  I should know because he is me.

So what is the point of this exercise?  Simply to show that there are many individuals who don’t fit the constricting molds that have become the new world views of “Conservative” and “Liberal”.  Moderates of both parties are an endangered species and any hint at compromise is seen as a betrayal of one’s political tribe.  I wrote a letter to the editor one time in which I decried the language of a local party boss who berated those of his own party who were not “pure enough”.  He also filled his commentary with plenty of invective for those of the other party and peppered it with lots of usage of the terms “Conservative” and “Liberal”.  A big part of my argument was that the use of labels in general was a lazy way to avoid making the effort to see other points of view.  In some ways it harkens back to what Sociologists like Erving Goffman called “Labeling Theory”.  The danger here is not only do we too easily reject those who do not fit the label but we also too easily help in the very creation of our own enemies.

So what is an old moderate to do?  Well, first and foremost is to not stoop to the levels of mudslinging that are so common today.  Second is to call BS when alternative facts are espoused by anybody of any political stripe.  But to do so requires that you have the actual facts at hand as proof.  It may not (and probably won’t) change the mind of the person or persons touting the alternative facts but it’s still somewhat comforting to know what the truth is.  Third is to stand up for people who need a voice or a hand.  There are those who get disenfranchised from not only the political process but from society as well.  I have worked hard for what I have but I know many people who have worked hard and have very little.  As someone who tries to follow the example of Christ I know that I should do what I can with what I have to help people who need it.  But I can’t help everyone and sometimes that breaks my heart.  I’m not saying you have to be a Christian or even believe in God to be part of the solution.  Your motivations may be different than mine and your resources may be different than mine but the results will be the same – someone who needs it will be helped.  Last is to vote.  For about ten years my wife and I lived in a state where our votes almost always went opposite of the majority.  But we voted anyway because we view voting as not just a right but a responsibility.  Besides, I always say that if you don’t vote then you have thrown away your right to complain about the results.

The United States is the greatest nation in the history of the world in terms of wealth, opportunity, and personal freedoms.  To have been born here is a stroke of fate for which I am eternally grateful.  The fact that I feel like an outcast from both political parties does not change that perspective.  But I hope, and pray, that I will live long enough to see reason and civility return to our political process.  Maybe then there will once again be a country for this old man.

I2C 4-Digit LED Interface

In an earlier project I detailed an I2C (two-wire) interface to the common 1602 LCD.  Recently I found a cheap 4-digit LED module that uses a pseudo I2C interface.  I don’t have a particular need for it but I went ahead and added one to a parts order I made.  If you know a little about I2C you know that it is a serial interface where the controlling circuit provides the clock and controls the direction of the single data line.  Usually you can hang several I2C modules on the same pair of line (e.g.: clock module and display module) because each module has a unique address.  I said this LED module has a “pseudo I2C” interface because it does not have an address.  That means that it needs to be the only device using the data line.

LED I2C Module

TM1637 Module

The module I have is similar to the one shown here.  It is typically listed as part number TM1637.  You can find a variety of information on the web but I had a hard time finding all of the details I needed.  I also looked at some existing code (Python and C) which can often be a dangerous thing to do.  The one critical thing I determined from the code was that the data bits sent to the LED module need to be LSB first.  That differed from the approach for the other I2C projects I have done.  Another idiosyncrasy that I noted is that the addresses for the digits go from left to right (starting with address C0).  That means that data must be written from most significant digit to least significant digit if using the auto increment addressing mode.  In fixed addressing mode you would just pick the appropriate digit and write directly to it.

I2C Communications

I2C communications are accomplished by using just two wires: one for data (SDA) and one for a clock (SCL).  The data line is defined as being bidirectional so there needs to be a “master” device on the bus to control things.  The clock rate is limited by the slowest device on the bus and controlled by the master device.  Basically it sets a data bit and then toggles the clock line.  At the end of each byte transfer the master reverses the direction of the data line and then clocks in the acknowledgement bit from the slave device.

There are three basic commands for the LED module.  One controls the on/off and brightness of the display, one controls the address of the digit to write, and one controls the addressing mode (fixed or auto increment).  Usually an application will just load all four digits each time so the auto increment mode is normally used and the address set to the first (left-most) digit.

Hardware

LED I2C Interface

The connections to the PIC are pretty simple with only two I/O pins being used.  That leaves four other pins on our little 12F683 for getting sensor data.  This same connection can be used with the 16F688 if we want more I/O pins.  The module already has the required pull up resistors on the SDA and SCL lines.

Software

The software link is listed below.  While it is targeted for the 12F683, it is easily ported to bigger versions of the PIC like the pin compatible 16F688 that I use for a lot of my projects.  Some of the newer PICs also have a hardware I2C interface so most of the related software routines would be greatly reduced or eliminated.

The I2C routines are the same as those I wrote for the LCD project but a lot of the other LCD-specific code was not needed for the LED module.  I added a lookup table that converts single packed ASCII characters to the required LED segments.  I also added a second test routine that shows how you can make non-ASCII displays by turning on the right segments.  The data bit associations for the segments are shown in the header of the software source code.  The eighth bit in the segment bytes is reserved for turning on the colon that resides between digits 2 and 3 on the module.  In my tests it appeared that all four characters had to include a 1 in the eighth bit in order for the colon to light.

I added a line of code to the end of the “i2cwaitack”routine to try and recover if the slave does not acknowledge the data transfer.  If the slave fails to acknowledge a transfer then the software will be reset by enabling the Watch Dog Timer (WDT).  Newer PIC chips have a software reset command that can be used instead.  An alternative for some I2C applications would be to simply make a call to “i2cstop” and then continue where the code left off.  That’s it for this post.  Check out my other electronics projects at boomerrules.com.

I2C LED