Trying to get Azimuth difference in a loop - micropython

I have a micropython program on a ESP32 connected to a stepper motor via a driver board.
The software runs in a while true loop. I'm trying to get the azimuth difference between 30 second previous azimuth reading and the post 30 second azimuth reading, then send the difference to the stepper motor. Unfortunately they will not let me post all my code, so here is a small example.....
import math
import time
import Stepper
from machine import Pin
s1 = Stepper.create(Pin(26,Pin.OUT),Pin(25,Pin.OUT),Pin(33,Pin.OUT),Pin(32,Pin.OUT),
delay=2)
while True:
xxxxx loop in here I have working code that updates the Azimuth of the sun in
realtime every 30 seconds XXXXXXX
s1.angle(azimuth, -1)
time.sleep(30)

Related

Raspberry Pi Pico - Generate finite length square wave

I have a question about generating square waves with finite length by using a Raspberry Pi Pico. For example, I want to generate a 20 kHz square wave with 100 periods, or to generate a 20 kHz square wave with an exact 1 ms. I cannot have accurate control over it.
To generate an infinite length of square waves is easy, as there are lots of examples online. I can use PIO to achieve it. For example, the following code could do so:
import rp2
from machine import Pin
#rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
def blink():
set(pins, 1)
set(pins, 0)
sm = rp2.StateMachine(0, blink, freq=25000, set_base=Pin(26))
sm.active(1)
However, I don't know how to accurately control the length/periods of the square wave. By using time.sleep() is not accurate at all.
Thank you in advance!
Use the "decrement X" instruction in the PIO to count the number of cycles you want. Might have to add some delays to get back a square wave. jmp(x_dec, "top_of_loop"). You'd hang, waiting for some input, and read the X value from the input FIFO. Then you'd run the square-wave loop, decrementing X. When it hits zero, you jump to the outer loop, where you again wait for a new X value for the number of cycles. I think that's it!

Trouble with 1602 LCD display displaying additional decimal points using a raspberry pi pico and DHT22 sensor as a weather station

I'm still new at coding and was able to put together some code from other sources for a simple weather station I built. Everything seems to be working fine but I do get the occasional hiccup where temperature readings on the display show additional decimals points beyond 0.1-0.9. I inserted a round command to round to the nearest decimal which I thought would take care of that but it still happens randomly. There are no issues when displaying temperature readings through the shell. It seems to add additional decimal points at random temperature readings just on the display. I've included pictures and code. Definitely would appreciate any help I can get figuring out this issue. Thank you.
from machine import Pin
import utime as time
from pico_i2c_lcd import I2cLcd
from machine import I2C
from DHT22 import DHT22
i2c = I2C(id=1,scl=Pin(27),sda=Pin(26),freq=100000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
dht_data = Pin(15,Pin.IN,Pin.PULL_UP)
dht_sensor=DHT22(dht_data,Pin(14,Pin.OUT),dht11=False)
while True:
T,H = dht_sensor.read()
#Converted to Fahrenheit (FT) for LCD display
FT = T*1.8+32
if T is None:
print(" sensor error")
else:
print("{:3.1f}'C {:3.1f}%".format(T,H))
#DHT22 not responsive if delay too short
time.sleep_ms(500)
lcd.clear()
lcd.move_to(0,0)
lcd.putstr('Temp:')
lcd.move_to(10,0)
#If using Celsius, change (FT) to (T) and "F" to "C" on line below
lcd.putstr(str(round(FT, 1))+"F")
lcd.move_to(0,1)
lcd.putstr('Humidity:')
lcd.move_to(10,1)
lcd.putstr(str(H)+"%")`
what it normally looks like
the hiccup that randomly appears

HC-SR-04 with STM32

SR04 ultrasonic distance sensor with stm32.
When I use it with STM32 I use it as bothedge with input capture. I measure the HIGH time of the Echo pin.
I thought it was a mistake
When I used Ardiuno with pulsein, I was dividing the time by 29.1 .My measurement was accurate enough, but when I divide it by 29.1 when using STM32, the measurement is very wrong. I did it by dividing by 58.2. This time I got more accurate values. What is the difference?
Thanks a lot.
It's right to divide the duration of the echo pulse by 58, as described in the datasheet as follows.
You can calculate the range through the time interval between sending trigger signal and
receiving echo signal. Formula: uS / 58 = centimeters or uS / 148 =inch; or: the
range = high level time * velocity (340M/S) / 2;
I tried to find an Arduino code which divides pulse duration with 29.1 or 29, I couldn't find it. Some posts computes the range by range=(duration/2)/29.1. I guess you've missed dividing duration by two. Double check your Arduino code, or post it if you want to be assured.

Sampling rate is very low with ADS1256 and raspberry pi

I am trying to get data samples from a sensor using a ADS1256 library with a Raspberry Pi High-Precision AD/DA Expansion Board on my RaspberryPi 2B
Now as mentioned in their code and datasheet it can take around 30,000 samples per second, but when I am running it, it was taking around 15 samples per second. After some modifications in code, it is taking around 470 samples per second.
I need atleast 1000-1500 samples per second.
Here again is the link to the ADS1256 code.
I tried to use this at a higher rate of speed. If you are waiting for DRDY pin to go low for on the order of milliseconds it isn't going to work. I had no luck in modifying the software. I tried to use this http://abyz.me.uk/lg/lgpio.html#lguSleep but I never could get the interrupt to activate on the change of DRDY. It seems that the person who wrote the sample program for the ADS1256 could not either. I looked at the sample program for the mpc3202. http://abyz.me.uk/lg/lgpio.html#lguSleep He does similar things; He sleeps for .2s between samples. That won't work for his sample rate. One problem is that the raspberry pi has no real-time clock. I tried some unix time routines and got back 0 as a result.

Why stepper motor power supply fluctuates

I am driving stepper motor using Raspberry Pi 3. I am using stepper motor driver Pololu A4988. A driver has potentiometer which we can adjust to provide specified current to the motor. My stepper motor current rating is 1 Amp and I am using 12 V 3 A power supply, but still when I start the motor power supply fluctuates. Even while using a 12V 5A power supply, the same problem is occurring. Please, Help and provide the solution.