Monday, April 9, 2018

RCWL-0516 Doppler Radar Sensor

I came across a neat little sensor that detects human presence using RF. It's called RCWL-0516. A quick google reveals that it is a doppler radar microwave motion sensor module. Mr. Andreas Spiess seems to have quite a comprehensive video on this kind of sensors. In his video Mr. Spiess shows five different modules.

1. RCWL-0516
2. WB3-12
3. HW-MS03
4. XYC-WB-G1
5. HFS-DC06

Another useful video. I have the first module. The part number on the chip is RCWL-9196. According to the internet it is similar to the popular BISS0001 chip in PIR motion sensor modules. So the magic happens somewhere else since the chip is similar in characteristic with the PIR module chip. Mr. Spiess expresses that the some clever switching of the transistor can yield the desired result although he is not aware of how this module actually works. Some people have observed with their spectrum analyzer that the module works around 3.2GHz. I did not check because I do not have the necessary equipment at the time of writing. I have found an informative document but I doubt it is the original datasheet since the prose style indicates that the writer is also experimenting with the module and documenting it. Nonetheless it is very useful so I'm providing the link here

Here's the pinout of the module taken from the aforementioned document:
1. Pin description

The 3V3 pin provides a steady 3.3V supply but the current delivering capability is unknown and the voltage is most likely generated inside the RCWL-9196 chip so I will not recommend using it. The VIN pin is actually the VCC and according to the documents floating around on the internet the supply voltage is 4 - 28V (what a wide range!) I have supplied the module with 5V for my test. The OUT pin cannot deliver much current so it is not recommended to attach loads directly to it. I used a BC547 to connect the module with an LED which is a bit unnecessary because the OUT pin can drive an LED albeit not very brightly. The CDS pin probably outputs the ambient light condition data.  

Apparently the range is close to 4 meters but I have yet to test it. It is omnidirectional. Supposedly, it can detect human movement through thin wood partitions and glass. Immediately after powering up the module, the output goes high for around 5 seconds and then low for around 5 seconds. During this time it is not possible to influence the result of this module no matter how still you stay or how much you move around, it will ignore you. After approximately ignoring you for around 5+5 = 10 s the module starts detecting. The sensitivity is quite good. It keeps the output pin high as long as it detects movement. According to Mr. Spiess the module consumes around 2.7 mA at 5 volts. The RCWL-0516 module has a connection for a CDS light dependent resistor. It is probably used to inhibit it's operation when the ambient light is already high. It is also possible to change the sensitivity or delay time of the module by connecting a resistor or a capacitor on the bottom of the board. 


2. Teeny tiny board


3. Simple to hook up


4. Top view


5. Bottom view

Wednesday, April 4, 2018

Useful commands and tricks for Raspbian Wheezy & Python

Useful python tutorial sites:

http://sthurlow.com/python/

https://www.learnpython.org/

Default Pi login credentials:
User: pi
Password: raspberry

1. To start GUI write startx and to exit GUI write pkill x

2. sudo apt-get update

3. sudo apt-get upgrade

4. sudo apt-get install python-requests

5. Installing pip for python 2
    sudo apt-get install python-pip

6. Installing libdbus
    sudo apt-get install libdbus-1-dev

7. sudo pip install pathlib

8. Type ls to see the content of the current directory

9. To see available WiFi networks
    sudo iwlist wlan0 scan | grep ESSID

10. To see the current network it's connected to
      sudo iwgetid

11. sudo iwconfig

12. sudo ifconfig

13. To write python code in terminal type
      python

14. To exit type
      raise SystemExit

15. To run a python code write
      python test.py

16. To exit
      ctrl+c

16. If the python script needs admin authority or accesses GPIO then you need sudo
      sudo python test.py

17. To run the script in a separate thread
      sudo python test.py &

18. To exit
      ctrl+d

19. To see the temperature, type
      vcgencmd measure_temp

20. To configure RPi, type
      sudo raspi-config

21. To pass audio through hdmi
      Edit /boot/config.txt
      hdmi_drive = 2

22. To hard code display resolution
      Edit /boot/config.txt
      hdmi_group = 2
      hdmi_mode = 39 (for 1360x768)
      Read this link for more details
   
23. To see the current resolution setting, type
      fbset -s

24. To see the attached storage
      df

25. To unmount flash drives
      sudo umount /dev/sda1

26. Sudo raspi-config → cli
      Cli in wheezy does not auto login
      sudo nano /etc/inittab
      1:2345:respawn:/sbin/getty --autologin pi --noclear 38400 tty1

      To blank the screen after a minute →
      /etc/kbd/config
      BLANK_TIME=1
      BLANK_DPMS=off
      POWERDOWN_TIME=0 (note: not sure about this line more details here →  link)

27. To clear boot messages when starting up append this bit to .bashrc file →
      sudo clear

28. To install requests, update and upgrade first
      sudo apt-get update
      sudo apt-get upgrade
      sudo apt-get install python-requests

29. To change the keyboard setting with
      sudo raspi-config
      Select a generic keyboard and change country to US

30. print the name of my working directory
      pwd

31. To access Pi via SSL you must enable SSL from raspi-config. To find the IP address and other network related information use the command "ifconfig". To find Pi in headless mode, connect Pi with an straight ethernet cable. Start up either Find My Pi tool by adafruit or Angry IP scanner and get the IP address of the Pi. Open putty and get in using the IP address. 

32. To install PIL for working with images and icons using Tkinter for Python 2
sudo apt-get install python-imaging
sudo apt-get install python-imaging-tk

33. Tkinter won't run from cli so you will have to start the desktop interface. To run codes at start up edit .bashrc file and append the command sudo python yourTkinterTest.py to run your code. Now go to raspi-config and select boot to desktop mode. To load up the terminal automatically on desktop boot edit the following file:
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
Append the following line at the end
@lxterminal

34. To disable Xsession (Desktop) from blanking add the following lines in the file mentioned in 33
@xset s noblank
@xset s off
@xset -dpms

35. Installing  Eclipse Paho MQTT Python client library:
sudo apt-get update
sudo apt-get install python-smbus
sudo apt-get install mosquitto
git clone https://github.com/eclipse/paho.mqtt.python
cd paho.mqtt.python
sudo python setup.py install

36. To forget a network or add a new one from cmd edit the following line:
/etc/wpa_supplicant/wpa_supplicant.conf

The format is as follows:
network = {
            ssid = ""
            psk = ""
            key_mgmt = WPA-PSK
}

Check this link for details.








Inside of a dead Xiomi 20,000 mAh power bank

Apparently, this Xiomi 20,000 mAh power bank started to heat up while charging and destroyed the Remax charger. Inside there's circuit board and two 10,000 mAh flat packs in parallel. I'm not sure whether they are Li-ion or LiPo. The battery is supposedly flat but I didn't measure it. The ugly wires were not originally attached.      


1. The cell packs on the Casing 

2. Side View

3. Spec Shot

4. Cell packs with casing 

5. The packs are quite thick

6. Cells, casing, top cover

7. Circuit board top view

8. Circuit board bottom view

9. Pretty Inductors


Tuesday, April 3, 2018

How to be well versed in Electronics


Always updating:



1. Concepts and laws of circuit like KVL, KCL, mesh, node, etc. both mathematically and practically. Also introduction to lab equipment like oscilloscope, function generator, power supplies, spectrum analyzer, programmable dc load, etc.

a.       KVL example circuit drawing, simulating, breadboard prototyping, mathematical calculations and practical measurements.

b.      KCL example circuit drawing, simulating, breadboard prototyping, mathematical calculations and practical measurements.

c.       Voltage and current divider example circuit drawing, simulating, breadboard prototyping, mathematical calculations and practical measurements.

d.      Four terminal sensing example circuit drawing, simulating, breadboard prototyping, mathematical calculations and practical measurements.

e.       Wheatstone bridge example circuit drawing, simulating, breadboard prototyping, mathematical calculations and practical measurements.

f.       Resistors, Capacitors and Inductors – fundamental theory and applications.

g.      Impedance, resistance, reactance, admittance, conductance, susceptance.



2. Diodes, BJTs, FETs and basic semiconductor devices along with their models.



a.                   Common diodes, Light Emitting Diode, Photodiode, Schottky Diode, Shockley diode, Varactor Diode or Varicap Diode, Zener Diode etc.

b.                  Common NPN and PNP diodes, N-channel and P-channel MOSFETs, JFETs, IGBT, Thyristors (SCRs, diac/triacs), etc.



3.      AC-DC sources and their applications.



a.       AC and DC power source fundamentals and their uses.

b.      Transformers, Rectifiers, ripples and filtering

c.       Theory and operation of linear and switch mode power supplies

i. Linear Voltage regulators

ii. DC-DC converters (Isolated/non-isolated, Buck/Boost/SMPS/Δ†uk/Sepic/Flyback converter)

d. Batteries and their safe usage (lead-acid, dry-cell, zinc carbon, alkaline, lithium ion, lithium polymer, nickel-cadmium, nickel metal hydride, etc.)

4. Applications of semiconductors (Diodes, FETs and BJTs) in real life scenes. Designing in PCB, Vero board, Strip board, etc.

5. Op Amps, comparators and 555 timers and their uses.

a. Operational amplifier basics, inverting, non-inverting amplifiers, non-inverting and inverting summing amplifier, differential amplifiers, differentiator, integrator, comparator, voltage follower, peak detector, oscillators etc.

b. Active filters, low pass, high pass, band pass, band reject filters with operational amplifiers.

c. Oscillators, Astable, monostable and bistable multivibrators and clock generators, Pulse width modulation, duty cycle, hysteresis.

6. Digital logic devices and designs.

a. DLD fundamentals and logic gates such as - AND, OR, NAND, NOR, XOR, XNOR

b. Binary arithmetic, truth tables, Karnaugh maps, Boolean algebra, floating point arithmetic, combinational circuits, Binary coded decimal, parity, gray code, etc.

c. Universal gates, adder circuits, subtractor circuits, multiplexer, demultiplexer, encoder, decoder, flips flops, latch circuits, edge triggered flip flops, counters, synchronous and asynchronous sequential circuits, state machine (table, diagram), shift registers.  

d. Introduction to and working with 74 series and 4000 series chips    

7. Measurement techniques for R, L, C. ADC, DAC and mathematical modelling and understanding.

8. Advanced analog and digital mixed systems, MEMs, Piezo materials, Transducers

ADC, DAC fundamentals such as resolution, quantization error, dither, jitter, sampling rate, aliasing, oversampling. Schemes such as direct conversion, successive approximation, ramp-compare, Wilkinson, integrating, delta integrated, sub ranging quantizer (pipelined), sigma-delta, intermediate FM stage, 

9. Basic telecom engineering concepts and applications like radios, DTMF, telephone, wireless communication (RF, Bluetooth, Wi-Fi, IR, NFC) etc. Encoding schemes such as Manchester coding, NRZ coding, etc. and Phillips NEC, RC5 for IR communication.

10. Application of linear systems in analog electronics.

11. Microprocessors, Micro controllers and other embedded systems. 

Atmel AVR, 8051, Intel 8086, STM8, STM32, Microchip PIC, TI MSP430 ultra low power series, Raspberry Pi, ESP8266, ESP32 etc.


12. Sensors & communicating with them.

a. Accelerometers, Gyro, Color, Light, Vibration, Flex, Fingerprint, Touch, Thermal, Temperature, Humidity, Moisture, IMUs, Pressure, Distance, Sound, Proximity, Motion, Altitude, Compass, Ultrasonic, Infrared, Force, Weight,

b. SPI, I2C, UART, USART, I2S, MODBUS, CAN, USB, One wire,

13. Input and output devices in embedded systems

            a. switches, buttons, matrix keypads, keyboard, mouse, touchscreen, rotary encoders,

            b. LEDs, bar graph displays, RGB LEDs, seven segment displays, LCDs, TFT displays,

14. Digital signal processing and its application in embedded systems (FIR and IIR filters).

15. Advanced microcontroller based systems and their design.

16. Application of control systems concepts like PID, fuzzy logic, Fourier, Laplace and Z transform in embedded systems.

17. Advanced programming concepts like RTOS and the basics of VLSI design.

18. Simulation software such as OrCAD, Multisim, Proteus etc.



















Ultrasonic distance sensors

There are 3 pin modules , 4 pin as well as 5 pin modules. These are the most common and inexpensive ones I have come across.


1. Ping Module
2. HC-SR04
3. DYP-ME007v1

HC-SR04 is an ultrasonic ranging sensor. This economical sensor provides
2cm to 400cm of non-contact measurement functionality with a ranging
accuracy that can reach up to 3mm. Each HC-SR04 module includes an
ultrasonic transmitter, a receiver and a control circuit.
There are four pins on the HC-SR04: VCC (Power), Trig (Trigger), Echo (Receive), and GND (Ground).

There's another variant called DYP-ME007. This module has 5 pins. 5 pin module
can be used as a 4 pin or 3 pin module. If you leave out the 4th pin  (OUT)  in
a  5 Pin Module , it behaves as a 4 pin Module. If you Ground the 4th pin then
it behaves as a 3 pin PING module. If 4th pin is connected to GND ,pin 2 is left out.
Now the  Trigger Input & Echo Output appears on single pin 3 (behaves as a 3 pin module).
So 4th pin of a 5pin module acts as a Mode Select pin.

I have tested both the 4 pin and 5 pin modules with RPi. Both performed similarly. The datasheet
claims these modules to have 2cm to 400 cm range but in practice it's effective upper range cuts
off at about 200 cm. The lower range is about 4 cm. The detection angle is around ±30°. The RPi being an MPU won't have accurate results as it's not strictly executing just my program and the timing will be a bit off. If accuracy is needed then MCU should be used as it offers tighter controls.

Unfortunately, I don't have the 3 pin ping module at hand for testing.

Here's my setup (ignore the SIM800L module it's for another project)


It should be noted that the ECHO pin is fed to the RPi via resistor dividers since the sensor is powered from 5V. Using simple resistors to shift the voltage level might introduce some inaccuracies but that is yet to be tested. It would be ideal to use a proper level shifter in my opinion but the accuracy was never of any concern in this project. 

Here's a quick and dirty demo code I wrote for RPi 3 Model B on Raspbian Wheezy distro. It might need further furnishing (probably does). ➤

πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
LED  = 27
u = 200 #upper threshold
l = 4         #lower threshold
count = 0

print "Measuring distance:"

GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
GPIO.setup(LED,  GPIO.OUT)
GPIO.output(LED, False)

def onCount():

global count
while count < 5:

GPIO.output(TRIG, False)
print "On processing..."
time.sleep(0.1)

GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)

while GPIO.input(ECHO) == 0:
pulse_start = time.time()

while GPIO.input(ECHO) == 1:
pulse_end = time.time()

pulse_duration = pulse_end - pulse_start

distance = pulse_duration * 17150

distance = round(distance, 2)

print "Distance: ", distance, "cm"

if int(distance) in range (l, u):
count += 1;
else:
count = 0;

pulse_start = 0
pulse_end    = 0
pulse_duration = 0

count = 0
GPIO.output(LED, True)

def offCount():

global count
while count < 5:

GPIO.output(TRIG, False)
print "Off processing..."
time.sleep(0.1)

GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)

while GPIO.input(ECHO) == 0:
pulse_start = time.time()

while GPIO.input(ECHO) == 1:
pulse_end = time.time()

pulse_duration = pulse_end - pulse_start

distance = pulse_duration * 17150

distance = round(distance, 2)

print "Distance: ", distance, "cm"

if int(distance) in range (l, u):
count = 0;
else:
count += 1;

pulse_start = 0
pulse_end    = 0
pulse_duration = 0

count = 0
GPIO.output(LED, False)

try:
while True:

onCount()
offCount()

finally:

GPIO.cleanup()

πŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”Ί

I actually plan on using this sensor to sense presence in front of a 46" LED TV from 1.5 m away. The TV will play and pause a video according to the viewer standing in front.

I shall post another post about that project when it's done.

Here's a demo code of how to play and pause videos on omxplayer which starts a video, pauses 3 seconds in to the video for 3 seconds and then plays it for 3 seconds and finally closes the video. There are probably far better ways to do this but this is the method that came to me at the time of writing. ➤

πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»πŸ”»

import os
import sys
import subprocess
from time import sleep
from subprocess import Popen

path = "/home/pi/video1.mp4"

def play_video():
omxprocess = subprocess.Popen(['omxplayer', '-b', '--no-osd', path], stdin=subprocess.PIPE, stdout=None, stderr=None, bufsize=0)
sleep(3)
omxprocess.stdin.write('p')
sleep(3)
omxprocess.stdin.write('p')
sleep(3)
omxprocess.stdin.write('q')

play_video()

πŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”ΊπŸ”Ί

I followed this tutorial ---------------------->

Tutorial 1

This one is useful too ----------------------->

Tutorial 2

I wonder whether ultrasonic bursts are harmful to humans. I haven't seen many articles on this topic although I didn't look thoroughly. It would be interesting to research on this topic.