# Tough to get exposure correct

**URL:** <https://forums.openmv.io/t/tough-to-get-exposure-correct/1329>\
**Category:** OpenMV Boards\
**Created:** [July 4, 2019, 2:51pm UTC](https://forums.openmv.io/t/tough-to-get-exposure-correct/1329 "2019-07-04T14:51:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ScottM](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@ScottM](https://forums.openmv.io/u/ScottM)\
**Post date:** [July 4, 2019, 2:51pm UTC](https://forums.openmv.io/t/tough-to-get-exposure-correct/1329/1 "2019-07-04T14:51:01Z")

</div>

I’m using the QRCode sample, modified slightly, to read a large QR Code. The problem I’m having is how difficult it is to get the exposure right so that the code can be read. Ideally, I want to use a properly tuned auto exposure, because I’m sure that the lighting conditions will change in my lab over the course of a day. The sun hits the windows for part of the day. I tried turning off autoexposure and setting things manually, but it didn’t work the way I thought it should.

Here is my code. You can see in the images that the QR Code is overexposed and the histogram shows it. Using manual exposure settings is only good if the environment is tightly controlled. I need a better autoexposure.

```python
import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((240, 240)) # look at center 240x240 pixels of the VGA resolution.
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False)      
sensor.set_auto_exposure(True)
#sensor.set_brightness(0) # range 0-31
#sensor.set_saturation(0) # range -3 to 3
#sensor.set_gainceiling(2) # 2,4,8,16,32,64,128
#sensor.set_contrast(2) # range -3 to 3
clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()
    for code in img.find_qrcodes():
        img.draw_rectangle(code.rect(), color = 127)
        print(code)

```

 ![histogram.PNG](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/bd08d012d3122cf619e2eda7d95daed2d10f54bf.png)  
 ![Find QR Code.jpg](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/5b82caa781bb263c65f348df98142059b57b7acb.jpeg)

---

<div class="post-metadata">

**Author:** ![anacoda](https://avatars.discourse-cdn.com/v4/letter/a/b38774/32.png) [@anacoda](https://forums.openmv.io/u/anacoda)\
**Post date:** [August 27, 2019, 3:59pm UTC](https://forums.openmv.io/t/tough-to-get-exposure-correct/1329/2 "2019-08-27T15:59:27Z")

</div>

Hi, I’m having the same problem.  
The code I currently have for changing the exposure is as follows:

Variables:

```python
ExpStep = 200
CurExp = 1000
MinBrt = 20
MaxBrt = 35
MinExp = 10
MaxExp = 60000

KP_High = 10
KP_Low = 100

```

And in the loop:

```python
clock.tick()
sensor.set_auto_exposure(False, CurExp)

sensor.skip_frames(4)
img = sensor.snapshot()
curStats = img.get_statistics()
CurBrt = curStats.l_mean()

if(CurBrt > MaxBrt):
    ErrHigh = CurBrt - MaxBrt
    ExpStep = KP_High * ErrHigh
    CurExp = max(CurExp - ExpStep, MinExp)
elif(CurBrt < MinBrt):
    ErrLow = MinBrt - CurBrt
    ExpStep = KP_Low * ErrLow
    CurExp = min(CurExp + ExpStep, MaxExp)

```

It _kind_ of works but it’s quite unreliable. Any suggestions?

Thanks,

Ana

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [August 27, 2019, 6:13pm UTC](https://forums.openmv.io/t/tough-to-get-exposure-correct/1329/3 "2019-08-27T18:13:16Z")

</div>

Hi, I made the camera driver better recently in the latest firmware about to come out. What boards do you have? I can upload the latest firmware for them here.
